gpt4 book ai didi

java - 尝试布局 JPanel。每次运行时Java程序布局都会改变

转载 作者:行者123 更新时间:2023-11-30 03:53:09 25 4
gpt4 key购买 nike

第一次发帖,请多多包涵。

我是 Java 新手,正在尝试让 3 个 JPanel 彼此对齐。第一个图像是我想要的样子,有时当我运行程序时它会出现,但正如您通过其他图像看到的那样,它并不是每次运行时都对齐。有时甚至不显示某些图像/组件。

那么如何让三个 JPanel 一个接一个垂直排列呢?

import java.awt.event.ActionListener;
import java.awt.*;

import javax.swing.*;

public class FrameMain {

static final int MY_MINIMUM = 0;

static final int MY_MAXIMUM = 100;

public static void main(String[] args) {


JFrame frame1 = new JFrame("Harvest Frame Test");
frame1.setVisible(true);
frame1.setSize(800,700);
frame1.setResizable(false);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Container Panel

JPanel container = new JPanel();
container.setSize(800,700);
container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
frame1.add(container);



//First Panel

JPanel panel1 = new JPanel();
panel1.setAlignmentX( Component.LEFT_ALIGNMENT );//0.0



container.add(panel1);

JButton button1 = new JButton("Add Water");
panel1.add(button1);
JButton button2 = new JButton("Add Food");
panel1.add(button2);
JButton button3 = new JButton("Add Medicine");
panel1.add(button3);

ImageIcon image = new ImageIcon("C:/Users/Nick/Documents/EclipseArt/plant.gif");
JLabel imagelabel = new JLabel(image);
panel1.add(imagelabel);

JProgressBar pbar = new JProgressBar();
pbar.setMinimum(MY_MINIMUM);
pbar.setMaximum(MY_MAXIMUM);
// add to JPanel
panel1.add(pbar);

// Second Panel

JPanel panel2 = new JPanel();
panel2.setAlignmentX( Component.LEFT_ALIGNMENT );//0.0


container.add(panel2);

JButton button4 = new JButton("Add Water");
panel2.add(button4);
JButton button5 = new JButton("Add Food");
panel2.add(button5);
JButton button6 = new JButton("Add Medicine");
panel2.add(button6);

ImageIcon image1 = new ImageIcon("C:/Users/Nick/Documents/EclipseArt/plant.gif");
JLabel imagelabel1 = new JLabel(image1);
panel2.add(imagelabel1);

JProgressBar pbar1 = new JProgressBar();
pbar1.setMinimum(MY_MINIMUM);
pbar1.setMaximum(MY_MAXIMUM);
// add to JPanel
panel2.add(pbar1);

// Third Panel

JPanel panel3 = new JPanel();
panel3.setAlignmentX( Component.LEFT_ALIGNMENT );//0.0

container.add(panel3);

JButton button7 = new JButton("Add Water");
panel3.add(button7);
JButton button8 = new JButton("Add Food");
panel3.add(button8);
JButton button9 = new JButton("Add Medicine");
panel3.add(button9);

ImageIcon image2 = new ImageIcon("C:/Users/Nick/Documents/EclipseArt/plant.gif");
JLabel imagelabel2 = new JLabel(image2);
panel3.add(imagelabel2);

JProgressBar pbar2 = new JProgressBar();
pbar2.setMinimum(MY_MINIMUM);
pbar2.setMaximum(MY_MAXIMUM);
// add to JPanel
panel3.add(pbar2);

}

//static class Action implements ActionListener {

//public void actionPerformed (ActionEvent e){



//}

//}

}

最佳答案

frame1.setVisible(true); 一直移动到底部。更改已经可见的框架上的组件可能会导致问题。

关于java - 尝试布局 JPanel。每次运行时Java程序布局都会改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23892716/

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