gpt4 book ai didi

java - 给JFrame添加两个JPanel,只有一个可见

转载 作者:行者123 更新时间:2023-11-30 08:06:28 28 4
gpt4 key购买 nike

Game

我有以下问题,当我想将 2JPanel 添加到我的 JFrame 时,只有一个可见,具体取决于我最后添加到框架的那个。我覆盖了两个 JPanel 上的 JPanel 默认 paintComponent() 方法。我该如何解决这个问题?

代码片段:边框:

public class BorderDrawer extends JPanel{
private int _width,_height;

BorderDrawer(int width,int height)
{
setOpaque(false);
_width = width;
_height = height;
}

@Override
protected void paintComponent(Graphics g) {
final int BUTTON_WIDTH = 20,BUTTON_HEIGHT = 20;
int MINES_HORIZONTALLY = _width;
int MINES_VERTICALLY = _height;
super.paintComponent(g);
try{
BufferedImage topLeftCorner = ImageIO.read(this.getClass().getResource("topLeftCorner.png"));
g.drawImage(topLeftCorner, 0, 0, null);
....// drawing other border components
}
}

时钟:

public class GraphicTimer extends JPanel{
Timer _aktTimer = null;
int seconds;
int _width = 0;
GraphicTimer(int width)
{
setSize(52, 31);
setOpaque(false);
_width = width;
int delay = 1000; //milliseconds
_aktTimer = new Timer(delay, taskPerformer);
}
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
try
{
final int BUTTON_WIDTH = 20,BUTTON_HEIGHT = 20;
int MINES_HORIZONTALLY = _width;
int HORIZONTAL_ENDING = 15+BUTTON_WIDTH*MINES_HORIZONTALLY;

BufferedImage clock = ImageIO.read(this.getClass().getResource("clock.png"));
g.drawImage(clock,HORIZONTAL_ENDING-54,22, null);
}
catch(IOException ex)
{
ex.printStackTrace();
}
}
....
}

JFrame:

public class DrawerField extends JFrame implements Serializable{  
//...
public DrawerField()
{
super("MineSweeper");
_FIELD = new Field();
constructorInit();
}
public void constructorInit()
{
_buttons = new FieldButton[_height][_width];
_isMouseEventEnabled = true;
fieldPanel = new JPanel();
smilePanel = new JPanel();
//INITS
int fieldSizeWidth = (_width)*20;
int fieldSizeHeight = (_height)*20; // Magic size
fieldPanel.setSize(fieldSizeWidth,fieldSizeHeight); // 20x20
fieldPanel.setLocation(15, 70);
fieldPanel.setLayout(new GridLayout(_width,_height));
int fullWindowWidth = fieldSizeWidth+36;
int fullWindowHeight = fieldSizeHeight+142;
setSize(fullWindowWidth,fullWindowHeight);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);

restartButton = new RestartButton(this);
smilePanel.setSize(34,34);
smilePanel.add(restartButton);
smilePanel.setLayout(new GridLayout(1,1));
smilePanel.setLocation((int)fullWindowWidth/2-(34/2)-1,20);
///INITIALS

for(int i = 0; i < _height; i++)
{
for(int j = 0; j < _width ; j++)
{
_buttons[i][j] = new FieldButton(_hidden[i][j],true,i,j,this);
fieldPanel.add(_buttons[i][j]);
}
}

add(smilePanel);
add(fieldPanel);

borderDrawer = new BorderDrawer(_width,_height);
_graphicTimer = new GraphicTimer(_width);
_graphicTimer.start();


add(_graphicTimer); // This is the two lines which change the result
add(borderDrawer);

MenuBar menuBar = new MenuBar(this);
setJMenuBar(menuBar);
setVisible(true);
}
//...
}

更简单和可编译的例子:

public class Main {

public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setSize(500, 500);

MyPanel panel1 = new MyPanel(30,30);
frame.add(panel1);
MyPanel panel2 = new MyPanel(70,30);
frame.add(panel2);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.repaint();
frame.setVisible(true);
}

public static class MyPanel extends JPanel
{
int _x,_y;
MyPanel(int x, int y)
{
_x = x; _y = y;
}
@Override
public void paint(Graphics g) {


g.drawOval(_x,_y,20,20);
}
}
}

我的主要目标是在不使用任何布局的情况下向 JFrame 添加 2 个圆圈。(如您在上面的示例中所见,我的 JFrame 上已经有很多东西,这就是我不想使用布局的原因)。这个例子中的问题是一样的,当我添加第二个圆圈时,第一个圆圈消失了。

最佳答案

很简单。在框架中添加面板会将其添加到框架的 content pane 中。此内容 Pane 的默认布局为 BorderLayout,这意味着每次添加面板时,它都会添加到内容 Pane 的中心并替换前一个面板。这就是为什么您只看到最后一个的原因。使用设置为您选择的布局的 Jpanel 总是好的,将需要在屏幕上显示的所有内容都放在该面板中。如果您不想这样做,您也可以从框架调用 getContentPane() 并使用返回的 JPanel 实例。

关于java - 给JFrame添加两个JPanel,只有一个可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34380335/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com