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

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

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

Не прикручиваеться к кнопке запуск класса . Не получаеться вывести часы в одном окне з кнопками . Главный класс.
Листинг программы
  1. package test;
  2. import java.awt.BorderLayout;
  3. import java.awt.Container;
  4. import java.awt.GridLayout;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.awt.event.KeyEvent;
  8. import java.util.Date;
  9. import javax.swing.Icon;
  10. import javax.swing.ImageIcon;
  11. import javax.swing.JButton;
  12. import javax.swing.JFrame;
  13. import javax.swing.JLabel;
  14. import javax.swing.JRootPane;
  15. import javax.swing.Timer;
  16. public class Test extends JFrame{
  17. public static void main(String args[]) {
  18. JFrame frame = new JFrame("SwingDefaultButton");
  19. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  20. ClockLabel clock = new ClockLabel();
  21. getContentPane().add(clock, BorderLayout.NORTH);
  22. Man ourMan= new Man();
  23. ourMan.Menit();
  24. Test ct = new Test();
  25. ct.setVisible(true);
  26. Container content = frame.getContentPane();
  27. content.setLayout(new GridLayout(2, 2));
  28. JButton button1 = new JButton("Ip");
  29. button1.setMnemonic(KeyEvent.VK_B);
  30. content.add(button1);
  31. Icon warnIcon = new ImageIcon("Warn.gif");
  32. JButton button2 = new JButton(warnIcon);
  33. content.add(button2);
  34. JButton button3 = new JButton("Warning", warnIcon);
  35. content.add(button3);
  36. String htmlButton = "<html><sup>HTML</sup> <sub><em>Button</em></sub><br>"
  37. + "<font color=\"#FF0080\"><u>Multi-line</u></font>";
  38. JButton button4 = new JButton(htmlButton);
  39. content.add(button4);
  40. JRootPane rootPane = frame.getRootPane();
  41. rootPane.setDefaultButton(button2);
  42. frame.setSize(300, 200);
  43. frame.setVisible(true);
  44. // ClockLabel clock = new ClockLabel();
  45. // getContentPane().add(clock, BorderLayout.NORTH);
  46. }
  47. //public static void main(String args[]) {
  48. //ClockTest ct = new ClockTest();
  49. //ct.setVisible(true);
  50. }
  51. //}
  52. class ClockLabel extends JLabel implements ActionListener {
  53. public ClockLabel() {
  54. super("" + new Date());
  55. Timer t = new Timer(1000, this);
  56. t.start();
  57. }
  58. public void actionPerformed(ActionEvent ae) {
  59. setText((new Date()).toString());
  60. }
  61. }
Вторичный
Листинг программы
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.net.*;
  5. public class Man {
  6. public static void main(String [] args) {
  7. public void Menit(){
  8. JFrame frame = new JFrame("Current IP");
  9. frame.addWindowListener(new WindowAdapter() {
  10. public void windowClosing(WindowEvent e) {
  11. System.exit(0);
  12. }
  13. });
  14. String IP = "";
  15. try {
  16. IP = InetAddress.getLocalHost().getHostAddress();
  17. }
  18. catch (Exception e) {
  19. IP = "Error finding IP";
  20. }
  21. JPanel panel = new JPanel();
  22. panel.setBorder(BorderFactory.createTitledBorder(" Current IP Address"));
  23. panel.add(new JLabel(" " + IP + " "));
  24. frame.getContentPane().add(panel);
  25. frame.pack();
  26. frame.setVisible(true);
  27. }
  28. }

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

textual
Листинг программы
  1. class Man {
  2.  
  3.  
  4.         public void Menit(){
  5.  
  6.             JFrame frame = new JFrame("Current IP");
  7.             frame.addWindowListener(new WindowAdapter() {
  8.                 public void windowClosing(WindowEvent e) {
  9.                     System.exit(0);
  10.                 }
  11.             });
  12.             String IP = "";
  13.             try {
  14.                 IP = InetAddress.getLocalHost().getHostAddress();
  15.             }
  16.             catch (Exception e) {
  17.                 IP = "Error finding IP";
  18.             }
  19.  
  20.             JPanel panel = new JPanel();
  21.  
  22.             panel.setBorder(BorderFactory.createTitledBorder("        Current IP Address"));
  23.             panel.add(new JLabel("          " + IP + "          "));
  24.  
  25.             frame.getContentPane().add(panel);
  26.  
  27.             frame.pack();
  28.  
  29.             frame.setVisible(true);
  30.         }
  31.     }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

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

12   голосов , оценка 3.917 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы