当我运行该程序时,窗口及其所有属性都是正确的,但是按钮不会显示,对我做错了什么的想法?
我有两个类窗口和TimeTable0:
这是窗口:
package timetable0;
import java.awt.event.ActionEvent;
import javax.swing.*;
public class window extends JFrame {
JButton bt1,bt2,bt3,bt4 = new JButton();
JPanel panel = new JPanel();
public void ventana() {
setResizable(false);
setTitle("Time Table");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,550);
setVisible(true);
bt1.setText("Show Grades");
bt2.setText("Show Time Table");
bt3.setText("");
bt4.setText("");
panel.add(bt1);
panel.add(bt2);
panel.add(bt3);
panel.add(bt4);
}
public void actions (){
bt1.addActionListener((ActionEvent e) -> {
System.out.println("");
});
bt2.addActionListener((ActionEvent e) -> {
System.out.println("");
});
bt3.addActionListener((ActionEvent e) -> {
System.out.println("");
});
bt4.addActionListener((ActionEvent e) -> {
System.out.println("");
});
}
}
这是TimeTable0:
package timetable0;
public class TimeTable0 {
public static void main(String[] args) {
window menu = new window();
menu.ventana();
menu.actions();
}
}
答案 0 :(得分:5)
您没有将JPanel
添加到框架中。
panel.add(bt1);
panel.add(bt2);
panel.add(bt3);
panel.add(bt4);
setContentPane(panel);
您未在Event Dispatch Thread上运行该程序:
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
window menu = new window();
menu.ventana();
menu.actions();
}
});
}
您没有创建所有四个按钮:
JButton bt1 = new JButton();
JButton bt2 = new JButton();
JButton bt3 = new JButton();
JButton bt4 = new JButton();
答案 1 :(得分:1)
如果您真的想将类扩展为JFrame,我会这样做。
public class Window extends JFrame {
JButton bt1,bt2,bt3,bt4; //Do initialization in the constructor, not here
JPanel panel;
public Window() {
createComponents();
addComponents();
initFrame();
}
private void createComponents(){
panel = new JPanel();
panel.setPreferredSize(new Dimension(800, 600));
bt1 = new JButton("Show Grades");
bt2 = new JButton("Show Time Table");
bt3 = new JButton("btn 3");
bt4 = new JButton("btn 4");
}
private void addComponents(){
panel.add(bt1);
panel.add(bt2);
panel.add(bt3);
panel.add(bt4);
add(panel);
}
private void initFrame(){
setResizable(false);
setTitle("Time Table");
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
}
}
通常我更喜欢将类扩展到JPanel而不是将其扩展到JFrame。之后,在main()中创建一个JFrame,并将自定义的JPanel添加到其中。
答案 2 :(得分:0)
您需要初始化按钮并将panel
添加到JFrame。另外,尝试将setVisible(true)
语句移动到ventana()
方法的末尾,以便在添加组件后显示。
public void Ventana() {
bt1 = new JButton("Show Grades");
bt2 = new JButton("Show Time Table");
bt3 = new JButton();
bt4 = new JButton();
...
panel.add(bt4);
add(panel);
setVisible(true); //moved from top
}
答案 3 :(得分:0)
你没有在那里显示太多代码,但你至少应该尝试这样做:
请注意JButton bt1,bt2,bt3,bt4 = new JButton();
与编写如下:
JButton bt1 = new JButton();
JButton bt2 = new JButton();
JButton bt3 = new JButton();
JButton bt4 = new JButton();
执行JButton bt1,bt2,bt3,bt4 = new JButton();
,您只为bt4
而不是其他人创建JButton对象。
您还忘记将JPanel添加到JFrame中。
add(panel); //Add panel to frame
答案 4 :(得分:0)
逐个初始化按钮:
JButton bt1 = new JButton();
JButton bt2 = new JButton();
JButton bt3 = new JButton();
JButton bt4 = new JButton();
向面板添加按钮后,将面板添加到框架中:
panel.add(bt1);
panel.add(bt2);
panel.add(bt3);
panel.add(bt4);
setContentPane(panel);