我想在我的GUI中显示一个12个面板按钮。但由于某种原因,他们没有出现。我现在不知道我的代码有什么问题。我还没有在我的动作执行方法中添加任何内容,但我会(现在我只专注于显示按钮)。那么有人能告诉我什么是错的吗?谢谢!
编辑*这就是我的GUI类中的所有内容,我想要显示两个画布的电梯对象(没有显示)
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;
}
}
答案 0 :(得分:4)
你覆盖了框架的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) {
}
}
}
答案 1 :(得分:3)
编辑:
之前(您提供的代码)
this.add(mainPanel);
for(int i=0; i<12; i++)
{
buttonList.add(new Button(""+i));
buttonPanel.add(buttonList.get(i));
buttonList.get(i).addActionListener(this);
}
this.setLayout(new BorderLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainPanel.add(buttonPanel, BorderLayout.CENTER);
之后:(在向其添加组件之前设置框架的布局)
this.setLayout(new BorderLayout()); // move this line
this.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, BorderLayout.CENTER);
答案 2 :(得分:2)
建议:
getPreferredSize()
,返回所需的维度。pack()
之前,您始终要调用setVisible(true)
。例如,
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
public class UI extends JFrame {
private static final Dimension PREF_SIZE = new Dimension(800, 800);
private ArrayList<JButton> buttonList = new ArrayList<JButton>();
private JPanel buttonPanel;
public UI() {
super("Elevators");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(12, 1));
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);
}
@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();
}
});
}
}
显示为,
编辑2
考虑将BorderLayout用于整个GUI,两个电梯图像可以放置在BorderLayout.LINE_START和BorderLayout.LINE_END位置。电梯JPanels可以使用空布局来允许elivator组件改变位置。中央JPanel也可以使用BoxLayout.PAGE_AXIS(垂直)中的BoxLayout。