我的Java Swing GUI遇到了麻烦 . 首先,我在其上创建了一个带有GridBagLayout的面板,并将所有标签添加到其中 . 但是,我还在另一个JPanel右侧创建了一个面板,它添加了一个按钮和2个滑块,这些滑块可能与标签匹配 .
问题是JLabel小于另一个面板右侧的组件,使它看起来像这样....
恩 . 水选项 - JSLIDER (jslider看起来更大)
我试图通过将 ipadx
添加到更大的值来使组件变大,并且我也会响应 .
这是代码:
package gui;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
public class Options extends JFrame{
//water option (button changes text on or off)
//make a button listener
JSlider renderDistance;
JSlider grassDensity;
JButton waterToggleButton;
JLabel rdTitle;
JLabel gdTitle;
JLabel wtTitle;
JButton buttonClose;
static final int RD_MIN_VALUE = 0;
static final int RD_MAX_VALUE = 1000;
static final int RD_INIT_VALUE = 500;
static final int GD_MIN_VALUE = 0;
static final int GD_MAX_VALUE = 1000;
static final int GD_INIT_VALUE = 500;
public Options() {
this.setTitle("Settings");
this.setSize(getMaximumSize());
this.setLocationRelativeTo(null);
createView();
this.setVisible(true);
}
private void createView() {
//Making panels and adding them to window*
JPanel pOptions = new JPanel();
this.add(pOptions);
//These are for the labels that I added so people know which option they are using
JPanel pOptionLabels = new JPanel(new GridBagLayout());
pOptions.add(pOptionLabels);
//These are for the middle columns, the objects like button and slider
JPanel pOptionObjects = new JPanel(new GridBagLayout());
pOptions.add(pOptionObjects);
//Making panels and adding them to window*
//Initializing Objects*
GridBagConstraints gbcLabels = new GridBagConstraints();
GridBagConstraints gbcObjects = new GridBagConstraints();
renderDistance = new JSlider(RD_MIN_VALUE, RD_MAX_VALUE, RD_INIT_VALUE);
grassDensity = new JSlider(GD_MIN_VALUE, GD_MAX_VALUE, GD_INIT_VALUE);
waterToggleButton = new JButton("On");
rdTitle = new JLabel("Render Distance");
gdTitle = new JLabel("Grass Density");
wtTitle = new JLabel("Water Terrain Visibility");
//Initializing Objects*
//Giving objects some attributes using methods*
renderDistance.setMinorTickSpacing(100);
renderDistance.setMajorTickSpacing(500);
renderDistance.setPaintTicks(true);
renderDistance.setPaintLabels(true);
grassDensity.setMinorTickSpacing(100);
grassDensity.setMajorTickSpacing(500);
grassDensity.setPaintTicks(true);
grassDensity.setPaintLabels(true);
gbcLabels.gridx = 0;
gbcLabels.gridy = 0;
gbcLabels.anchor = GridBagConstraints.LINE_START;
gbcObjects.gridx = 0;
gbcObjects.gridy = 0;
waterToggleButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(waterToggleButton.getText().equals("Off")) {
waterToggleButton.setText("On");
}else {
waterToggleButton.setText("Off");
}
}
});
//Giving objects some attributes using methods*
//Add things to panel ex. p.add();
pOptionLabels.add(rdTitle, gbcLabels);
gbcLabels.gridy++;
pOptionLabels.add(gdTitle, gbcLabels);
gbcLabels.gridy++;
pOptionLabels.add(wtTitle, gbcLabels);
gbcObjects.gridx++;
pOptionObjects.add(renderDistance, gbcObjects);
gbcObjects.gridy++;
pOptionObjects.add(grassDensity, gbcObjects);
gbcObjects.gridy++;
pOptionObjects.add(waterToggleButton, gbcObjects);
}
public static void main(String[] args) {
new Options();
}
}
2 回答
改变你的想法 . 不要试图将所有标签和所有“其他”组件布局在单独的容器中,而是考虑将它们全部放在同一个容器中 .
这样,在布局时,布局计算与单个上下文中的所有组件相关 .
您遇到的问题在技术上是正确的,因为您有两个JPanels,其中包含两个独立的GridBagLayout管理器实例,因此一个布局管理器中的行(和列)没有关于另一个布局管理器中的行的信息 .
如果您打算在其中使用JComponents并创建一个类似外观的表,那么单个GridBagLayout就是您的选择 . 您可以稍后添加其他JPanels(使用不同的布局管理器或更多的GridBagLayouts!)但是在您的主GridBagLayout JPanel中 .
GridBagLayout是一个非常强大的布局,我建议坚持使用is并掌握它,因为它可以帮助你很好地(和快速地)布局事物 .
这是了解有关GridBagLayout的更多信息的绝佳资源:
https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html
一个小评论:IMO GridBagLayout不是用作“根”布局容器的最佳布局管理器 . 我建议创建一个带有BorderLayout()的“root”JPanel,并将你的GridBagLayout JPanel添加到BorderLayout面板的中心添加(yourGridPanel,BorderLayout.CENTER),因为BorderLayout通常免费提供水平填充的东西/垂直方向,让你可以很好地将窗户打包在中心(以后可以在NORTH / SOUTH / EAST / WEST两侧添加更多东西) .
祝好运!