简体   繁体   English

Java Swing按钮定位

[英]Java Swing Button Positioning

The situation here is the following: I have a JFrame, in which I store a list of images. 这里的情况如下:我有一个JFrame,在其中存储图像列表。 When the right-key is pressed, I display the next image in this fullscreen JFrame. 当按下右键时,我将在此全屏JFrame中显示下一张图像。 I want a Exit JButton in the upper right corner of the screen, which is the problem. 我想要屏幕右上角的Exit JButton,这就是问题所在。 To achieve that, I create a new JPanel everytime the image is changed, remove the old JPanel from the frame, and add the JButton to the new JPanel. 为此,每次更改图像时,我都会创建一个新的JPanel,从框架中删除旧的JPanel,然后将JButton添加到新的JPanel中。 The issue is the following: when I start the program, the JButton is in the middle of the JFrame. 问题如下:启动程序时,JButton位于JF​​rame的中间。 Once I load the next Image by pressing the right key the JButton is on the right position. 一旦我通过按右键加载了下一个Image,JButton就位于正确的位置。 However, workarounds like calling the method to display the next Image fail. 但是,解决方法(如调用方法以显示下一个图像)失败。 Even if I set the JButton invisible until the right key was pressed and the second image loaded, it still will be in the center of the screen instead of the upper right. 即使我将JButton设置为不可见,直到按下右键并加载第二张图像,它仍将位于屏幕的中心而不是右上方。 The following code is used: 使用以下代码:

BufferedImage image = ImageIO.read(images.get(number));
JPanel panel = new JPanel() {
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, null);
    }
};
if (currentPanel != null) {
    this.remove(currentPanel);
}
panel.setSize(Gallery.this.getSize());
currentPanel = this.add(panel);
jButton1.setBounds(Gallery.this.getWidth() - jButton1.getWidth(), 0, jButton1.getWidth(), jButton1.getHeight());
panel.add(jButton1);
currentPanel.repaint();

This code is executed once at startup. 该代码在启动时执行一次。 The JButton then is in the middle. 然后,JButton在中间。 It is executed again when loading the next Image, JButton now is in correct position. 加载下一个Image时,它将再次执行,JButton现在处于正确位置。

I already tried many things, like adding the JButton to the JFrame instead, setting JPanels layout to null (makes button invisible), repaint, pack, invalidate, nothing I try seems to work. 我已经尝试了很多事情,例如将JButton添加到JFrame,将JPanels布局设置为null(使按钮不可见),重新绘制,打包,无效,我尝试做的一切似乎都没有。 Is anyone able to instruct Swing to place that JButton in the upper right corner of my JFrame? 有人可以指示Swing将JButton放在我的JFrame的右上角吗? Thank you a lot! 非常感谢!

After cleaning up the mess I programmed there, an easy solution proved to work: I added a custom JPanel to the JFrame at startup and set a fixed width to the JButton. 在清理了我在那里编程的混乱之后,一个简单的解决方案被证明是可行的:我在启动时向JFrame添加了自定义JPanel,并为JButton设置了固定宽度。 Here the JPanel: 这是JPanel:

public class ImagePanel extends javax.swing.JPanel {
Image image;

public Image getImage() {
    return image;
}

public void setImage(Image image) {
    this.image = image;
}

public ImagePanel() {
    initComponents();
}

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawImage(image, 0, 0, this);
}                  

} }

I set a fixed width to the JButton and added it to that JPanel: 我为JButton设置了固定宽度,并将其添加到该JPanel中:

    jButton1 = new JButton();
    jButton1.setText("Exit");
    imagePanel = new ImagePanel();
    imagePanel.setSize(Gallery.this.getSize());
    add(imagePanel);
    imagePanel.add(jButton1);
    jButton1.setBounds(Gallery.this.getWidth() - 50, 0, 50, 30);
    jButton1.addActionListener((ActionEvent e) -> exit());
    displayImage(0);

The displayImage method consists of these lines: displayImage方法包含以下几行:

 BufferedImage image = ImageIO.read(images.get(number));
 imagePanel.setImage(image);
 imagePanel.repaint();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM