Java 面板没有';没有边界
我正在做一个项目,我刚刚开始使用GUI。因为这不是我最喜欢的话题,我很快就发现了一些不太好用的东西。所有内容(PacmanGrid、PacmanScore)都显示正确,但我为PacmanScore面板编写的边框除外!不管怎么说,这是代码,希望有人能帮上忙Java 面板没有';没有边界,java,swing,user-interface,jpanel,border,Java,Swing,User Interface,Jpanel,Border,我正在做一个项目,我刚刚开始使用GUI。因为这不是我最喜欢的话题,我很快就发现了一些不太好用的东西。所有内容(PacmanGrid、PacmanScore)都显示正确,但我为PacmanScore面板编写的边框除外!不管怎么说,这是代码,希望有人能帮上忙 public class PacmanFrame extends JFrame{ public PacmanFrame() { this.setLayout(new BorderLayout()); this.setTit
public class PacmanFrame extends JFrame{
public PacmanFrame() {
this.setLayout(new BorderLayout());
this.setTitle("Pacman");
PacmanGrid p1=new PacmanGrid();
PacmanScore p2 = new PacmanScore();
this.add(p1,BorderLayout.CENTER);
this.add(p2,BorderLayout.EAST);
super.setDefaultCloseOperation(EXIT_ON_CLOSE);
super.repaint();
pack();
super.setVisible(true);
}
public static void main(String[] args) {
PacmanFrame p1 = new PacmanFrame();
}
}
PacmanScore
public class PacmanScore extends JPanel{
private TitledBorder t3 = BorderFactory.createTitledBorder("Menu");
private Border etched = BorderFactory.createEtchedBorder(Color.WHITE, Color.white);
public PacmanScore() {
setLayout(new FlowLayout());
setPreferredSize(new Dimension(100,800));
setBackground(Color.DARK_GRAY);
t3.setBorder(etched);
setBorder(t3);
setVisible(true);
setOpaque(true);
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
super.paintComponent(g2);
g2.setColor(Color.white);
g2.drawString("Score: ", 20, 400);
}
}
PacmanGrid还通过面板进行扩展,并使用预定义的模式绘制经典的PacmanGrid。但我不认为这是相关的,因为问题显然在PacmanScore小组中。如果有人需要,我会发布代码。
提前谢谢
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
super.paintComponent(g2);
g2.setColor(Color.white);
g2.drawString("Score: ", 20, 400);
}
您没有正确重写paint(),因为您没有调用super.paint(),因此没有绘制边框
不要覆盖paint()。通过覆盖paintComponent()
有关更多信息,请阅读上的Swing教程部分
你为什么还要做定制绘画?只需在面板中添加一个JLabel
此外,Swing组件(顶层窗口除外)默认可见,因此无需使面板可见。谢谢,就是这样!多谢各位