我写了这段代码来显示一个带有三个按钮的窗口。但是,窗口出现但不显示按钮。这是代码:
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.*;
import java.awt.*;
public class Test3 extends JFrame{
public Test3() {
JFrame frame = new JFrame("Exemple");
JButton button1 = new JButton("Règles");
button1.setBounds(100, 60, 100, 30);
JButton button2 = new JButton("Jouer");
JButton button3 = new JButton("Scores");
JPanel pane1 = new JPanel(new GridLayout(0, 1));
this.setVisible(true);
pane1.add(button1);
pane1.add(button2);
pane1.add(button3);
frame.getContentPane().add(pane1, BorderLayout.EAST);
this.setVisible(true);
setSize(800, 600);
}
public static void main(String[] args) {
Test3 test3 = new Test3();
}
}
有没有人知道如何解决这个问题?
感谢您的帮助!
Agnès
答案 0 :(得分:2)
你有两个窗口......
public class Test3 extends JFrame{
public Test3() {
JFrame frame = new JFrame("Exemple");
您正在向frame
添加内容,但显示Test3
...
作为一般经验法则,您应该避免从JFrame
等顶级容器扩展,而是在需要时创建实例。这将使您从给定的实现中解锁,并允许您灵活地决定如何重用给定的组件,作为示例。
public class Test3 {
public Test3() {
JFrame frame = new JFrame("Exemple");
JButton button1 = new JButton("Règles");
JButton button2 = new JButton("Jouer");
JButton button3 = new JButton("Scores");
JPanel pane1 = new JPanel(new GridLayout(0, 1));
pane1.add(button1);
pane1.add(button2);
pane1.add(button3);
frame.getContentPane().add(pane1, BorderLayout.EAST);
frame.pack();
frame.setVisible(true);
}
可运行的例子......
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test3 {
public Test3() {
JFrame frame = new JFrame("Exemple");
JButton button1 = new JButton("Règles");
button1.setBounds(100, 60, 100, 30);
JButton button2 = new JButton("Jouer");
JButton button3 = new JButton("Scores");
JPanel pane1 = new JPanel(new GridLayout(0, 1));
pane1.add(button1);
pane1.add(button2);
pane1.add(button3);
frame.getContentPane().add(pane1, BorderLayout.EAST);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
Test3 test3 = new Test3();
}
});
}
}
答案 1 :(得分:0)
请尝试这样,我已更正
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.*;
import java.awt.*;
public class Test3 extends JFrame{
public Test3() {
super("Exemple");
JButton button1 = new JButton("Règles");
button1.setBounds(100, 60, 100, 30);
JButton button2 = new JButton("Jouer");
JButton button3 = new JButton("Scores");
JPanel pane1 = new JPanel(new GridLayout(0, 1));
this.setVisible(true);
pane1.add(button1);
pane1.add(button2);
pane1.add(button3);
getContentPane().add(pane1, BorderLayout.EAST);
this.setVisible(true);
setSize(800, 600);
}
public static void main(String[] args) {
Test3 test3 = new Test3();
}
}