Не получаеться вывести часы в одном окне с кнопками - Java

Узнай цену своей работы

Формулировка задачи:

Не прикручиваеться к кнопке запуск класса . Не получаеться вывести часы в одном окне з кнопками . Главный класс.
package test;
 
import java.awt.BorderLayout;
 import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Date;
 
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRootPane;
import javax.swing.Timer;
 
public class Test extends JFrame{
    
  public static void main(String args[]) {
    JFrame frame = new JFrame("SwingDefaultButton");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ClockLabel clock = new ClockLabel();
    getContentPane().add(clock, BorderLayout.NORTH);
    
    Man ourMan= new Man();
    ourMan.Menit();
    
    Test ct = new Test();
    ct.setVisible(true);
 
    Container content = frame.getContentPane();
    content.setLayout(new GridLayout(2, 2));
 
    JButton button1 = new JButton("Ip");
    button1.setMnemonic(KeyEvent.VK_B);
    content.add(button1);
 
    Icon warnIcon = new ImageIcon("Warn.gif");
    JButton button2 = new JButton(warnIcon);
    content.add(button2);
 
    JButton button3 = new JButton("Warning", warnIcon);
    content.add(button3);
 
    String htmlButton = "<html><sup>HTML</sup> <sub><em>Button</em></sub><br>"
        + "<font color=\"#FF0080\"><u>Multi-line</u></font>";
    JButton button4 = new JButton(htmlButton);
    content.add(button4);
 
    JRootPane rootPane = frame.getRootPane();
    rootPane.setDefaultButton(button2);
 
    frame.setSize(300, 200);
    frame.setVisible(true);
    
   // ClockLabel clock = new ClockLabel();
   // getContentPane().add(clock, BorderLayout.NORTH);
  }
 
  //public static void main(String args[]) {
    //ClockTest ct = new ClockTest();
    //ct.setVisible(true);
  }
//}
class ClockLabel extends JLabel implements ActionListener {
 
  public ClockLabel() {
    super("" + new Date());
    Timer t = new Timer(1000, this);
    t.start();
  }
 
  public void actionPerformed(ActionEvent ae) {
    setText((new Date()).toString());
  }
}
Вторичный
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
 
public class Man {
   public static void main(String [] args) {
       
       public void Menit(){
       
      JFrame frame = new JFrame("Current IP");
      frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
      String IP = "";
      try {
       IP = InetAddress.getLocalHost().getHostAddress();
      }
      catch (Exception e) {
       IP = "Error finding IP";
      }
      
      JPanel panel = new JPanel();
      
      panel.setBorder(BorderFactory.createTitledBorder("        Current IP Address"));
      panel.add(new JLabel("          " + IP + "          "));
      
      frame.getContentPane().add(panel);
    
      frame.pack();
     
      frame.setVisible(true);
   }
}

Решение задачи: «Не получаеться вывести часы в одном окне с кнопками»

textual
Листинг программы
class Man {
 
 
        public void Menit(){
 
            JFrame frame = new JFrame("Current IP");
            frame.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
            String IP = "";
            try {
                IP = InetAddress.getLocalHost().getHostAddress();
            }
            catch (Exception e) {
                IP = "Error finding IP";
            }
 
            JPanel panel = new JPanel();
 
            panel.setBorder(BorderFactory.createTitledBorder("        Current IP Address"));
            panel.add(new JLabel("          " + IP + "          "));
 
            frame.getContentPane().add(panel);
 
            frame.pack();
 
            frame.setVisible(true);
        }
    }

Оцени полезность:

12   голосов , оценка 3.917 из 5
Похожие ответы