我希望在我的 GUI 中显示一个包含 12 个按钮的面板。但出于某种原因,他们没有出现。我现在不知道我的代码有什么问题。我还没有在我的 Action 执行方法中放任何东西,但我会的(现在我只专注于让按钮显示)。那么有人可以告诉我出了什么问题吗?谢谢!
编辑* 这就是我的 GUI 类中的所有内容,我想要显示电梯对象的两个 Canvas (没有显示)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
public class GUI extends JFrame implements ActionListener
{
private static final Dimension PREF_SIZE = new Dimension(1000, 1000);
MyCanvas leftCanvas = new MyCanvas();
MyCanvas rightCanvas = new MyCanvas();
ArrayList<JButton> buttonList = new ArrayList<JButton>();
JPanel buttonPanel, leftPanel, rightPanel;
public GUI()
{
super("Elevators");
//setSize(800,800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//setVisible(true);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(1,3));
leftPanel = new JPanel();
leftPanel.add(leftCanvas);
rightPanel = new JPanel();
rightPanel.add(rightCanvas);
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(12,1));
buttonPanel.setSize(900,900);
add(mainPanel);
for(int i=0; i<12; i++)
{
buttonList.add(new JButton(""+i));
JButton btn = buttonList.get(i);
buttonPanel.add(btn);
}
mainPanel.add(buttonPanel, BorderLayout.CENTER);
mainPanel.add(leftPanel, BorderLayout.EAST);
mainPanel.add(rightPanel, BorderLayout.WEST);
createAndShowGui();
}
@Override
public Dimension getPreferredSize() {
return PREF_SIZE;
}
private static void createAndShowGui() {
UI frame = new UI();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
//public static void main(String[] args) {
//SwingUtilities.invokeLater(new Runnable() {
// public void run() {
// createAndShowGui();
// }
//});
//}
//public void paint(Graphics g)
//{
// }
public void actionPerformed(ActionEvent e)
{
}
}
这是我的 Canvas 类
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JButton;
public class MyCanvas extends Canvas
{
private Elevator e;
int xPos =0;
int yPos=0;
public MyCanvas()
{
setSize(600,600);
repaint();
}
public void paint(Graphics g)
{
g.setColor(Color.BLACK);
g.fillRect(xPos,yPos,100, 100);
}
public void actionPerformed(ActionEvent e)
{
repaint();
}
public void setElevator(Elevator ev)
{
e = ev;
}
}
最佳答案
您覆盖了框架的 paint
方法...
public void paint(Graphics g)
{
}
但是未能调用super.paint(g)
....
paint
的职责之一是绘制子组件...
已更新
此外,调用 this.setLayout(new BorderLayout())
会扰乱您的布局,因为约束已被丢弃,这意味着布局管理器不知道应该去哪里
更新示例
我能够制作这个...
有了这个
public class Elevator02 {
public static void main(String[] args) {
new Elevator02();
}
public Elevator02() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
UI ui = new UI();
ui.setLocationRelativeTo(null);
}
});
}
public class UI extends JFrame implements ActionListener {
ArrayList<Button> buttonList = new ArrayList();
JPanel buttonPanel;
public UI() {
super("Elevators");
setSize(200, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(1, 3));
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(12, 1));
add(mainPanel);
for (int i = 0; i < 12; i++) {
buttonList.add(new Button("" + i));
buttonPanel.add(buttonList.get(i));
buttonList.get(i).addActionListener(this);
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainPanel.add(buttonPanel);
}
// public void paint(Graphics g) {
// }
public void actionPerformed(ActionEvent e) {
}
}
}
关于Java GUI 类按钮面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13060476/