2

我有一个 JFrame,我将布局设置为 GroupLayout。

我正在添加两个 Jpanel,即 workingPanel(red) 、 backgroundPanel(green) 。

我希望绿色面板的高度更小,比如 50 或 60。我已将 backgroundPanel 的大小设置为 50,但在将其添加到 Jframe 时,backgroundPanel 的高度与 workingPanel 相同。在此处输入图像描述

代码是`import javax.swing。; 导入 java.awt。;

public class Home extends JFrame{

JButton b1;
JPanel workingPanel,backgroundPanel;

public Home(){

    new JFrame("Restaurant Billing");
    b1=new JButton("Hello");
    workingPanel=new JPanel(); 
    backgroundPanel=new JPanel();
    int maximumWidth=getContentPane().getWidth();
    backgroundPanel.setSize(maximumWidth,60);
    workingPanel.setBackground(Color.red); //workingpanel backgroundcolor is red
    backgroundPanel.setBackground(Color.green);//backgroundPanle backcolor is green
    //creating grouplayout and setting to mainframe
    GroupLayout layout=new GroupLayout(getContentPane());
    getContentPane().setLayout(layout);

    layout.setHorizontalGroup(
        layout.createParallelGroup()
        .addComponent(backgroundPanel)
        .addComponent(workingPanel)
    );
      layout.setVerticalGroup(
        layout.createSequentialGroup()
        .addComponent(backgroundPanel)
        .addComponent(workingPanel)

      );


    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}
public void launchFrame(){

    this.setVisible(true);
}
}

请帮助我。

4

3 回答 3

0

尝试使用 prefferedSize 属性。

backgroundPanel.setPrefferedSize(maximumWidth,60);
于 2013-08-13T12:32:21.753 回答
0

为了调整大小,我查看了类属性,找不到任何东西,可以触发。最好的解决方案是这样的。

package groupleyaut;

import java.awt.EventQueue;
import javax.swing.JFrame;
import  javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class gfg {
`enter code here`private JFrame frame;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                gfg window = new gfg();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public gfg() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 807, 325);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel();
    panel.setBackground(Color.BLACK);
    frame.getContentPane().add(panel, BorderLayout.CENTER);

    JPanel panel_1 = new JPanel();
    panel_1.setPreferredSize(new Dimension(10, 100));

    JPanel panel_2 = new JPanel();
    panel_2.setPreferredSize(new Dimension(200, 50));
    GroupLayout gl_panel = new GroupLayout(panel);
    gl_panel.setHorizontalGroup(
        gl_panel.createParallelGroup(Alignment.LEADING)
            .addGroup(gl_panel.createSequentialGroup()
                .addContainerGap()
                .addComponent(panel_1, GroupLayout.PREFERRED_SIZE, panel_1.getPreferredSize().width, GroupLayout.PREFERRED_SIZE)
                .addGap(64)
                .addComponent(panel_2, GroupLayout.PREFERRED_SIZE, 492, GroupLayout.PREFERRED_SIZE)
                .addContainerGap(166, Short.MAX_VALUE))
    );
    gl_panel.setVerticalGroup(
        gl_panel.createParallelGroup(Alignment.LEADING)
            .addGroup(gl_panel.createSequentialGroup()
                .addContainerGap()
                .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                    .addComponent(panel_2, GroupLayout.PREFERRED_SIZE, 128, GroupLayout.PREFERRED_SIZE)
                    .addComponent(panel_1, GroupLayout.PREFERRED_SIZE, panel_1.getPreferredSize().height, GroupLayout.PREFERRED_SIZE))
                .addGap(66))
    );
    panel_2.setLayout(null);

    JButton btnNewButton = new JButton("New button");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            panel_1.setPreferredSize(new Dimension(10, 200));
            GroupLayout gl_panel = new GroupLayout(panel);
            gl_panel.setHorizontalGroup(
                gl_panel.createParallelGroup(Alignment.LEADING)
                    .addGroup(gl_panel.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(panel_1, GroupLayout.PREFERRED_SIZE, panel_1.getPreferredSize().width, GroupLayout.PREFERRED_SIZE)
                        .addGap(64)
                        .addComponent(panel_2, GroupLayout.PREFERRED_SIZE, 492, GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(166, Short.MAX_VALUE))
            );
            gl_panel.setVerticalGroup(
                gl_panel.createParallelGroup(Alignment.LEADING)
                    .addGroup(gl_panel.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                            .addComponent(panel_2, GroupLayout.PREFERRED_SIZE, 128, GroupLayout.PREFERRED_SIZE)
                            .addComponent(panel_1, GroupLayout.PREFERRED_SIZE, panel_1.getPreferredSize().height, GroupLayout.PREFERRED_SIZE))
                        .addGap(66))
            );
            panel.setLayout(gl_panel);

        }
    });
    btnNewButton.setBounds(147, 42, 89, 23);
    panel_2.add(btnNewButton);
    panel.setLayout(gl_panel);


}

}

于 2020-05-06T05:29:29.853 回答
0

尝试使用“setMaximumSize()”方法。这将防止组件高度超出所需范围。如果希望保持组件宽度等于框架的宽度,则可以使用“getMaximumSize().width”来指定组件宽度。如果手动调整框架大小,这将允许组件调整为全框架宽度。

backgroundPanel.setMaximumSize(new Dimension(backgroundPanel.getMaximumSize().width, 60));

完整代码:

import javax.swing.*;
import java.awt.*;
public class Home extends JFrame{
    JButton b1;
    JPanel workingPanel,backgroundPanel;
    public Home(){

        new JFrame("Restaurant Billing");
        setPreferredSize(new Dimension(500,500));
        setSize(new Dimension(500,500));
        b1=new JButton("Hello");
        workingPanel=new JPanel(); 
        backgroundPanel=new JPanel();
        int maximumWidth = backgroundPanel.getMaximumSize().width;
        backgroundPanel.setMaximumSize(new Dimension(maximumWidth, 60));
        workingPanel.setBackground(Color.red); //workingpanel backgroundcolor is red
        backgroundPanel.setBackground(Color.green);//backgroundPanle backcolor is green
        //creating grouplayout and setting to mainframe
        GroupLayout layout=new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);

        layout.setHorizontalGroup(
            layout.createParallelGroup()
            .addComponent(backgroundPanel)
            .addComponent(workingPanel)
        );
          layout.setVerticalGroup(
            layout.createSequentialGroup()
            .addComponent(backgroundPanel)
            .addComponent(workingPanel)

          );


        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
    public void launchFrame(){

        this.setVisible(true);
    }
    public static void main(String args[]) {
        Home home = new Home();
        home.launchFrame();
    }
}
于 2021-09-13T06:19:00.813 回答