我有一个JFrame,我将布局设置为GroupLayout。
我正在添加两个Jpanel,即workingPanel(红色),backgroundPanel(绿色)。
我希望绿色面板的高度更低,例如50或60.i已将backgroundPanel的大小设置为50,但在将其添加到Jframe时,backgroundPanel的高度与workingPanel相同。
代码是`import javax.swing。; import 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);
}
}
Plz帮帮我。
答案 0 :(得分:4)
答案 1 :(得分:0)
尝试使用prefferedSize属性。
backgroundPanel.setPrefferedSize(maximumWidth,60);
答案 2 :(得分: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);
}
}