Tag: boxlayout

使用JComboBox的Swing BoxLayout问题而不使用setXXXSize

这是一个SSCCE: import java.awt.Color; import java.awt.Dimension; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JPanel; public class BoxLayoutTest extends JFrame { public BoxLayoutTest(){ JPanel main = new JPanel(); main.setLayout(new BoxLayout(main, BoxLayout.Y_AXIS)); main.setBackground(Color.red); this.add(main); JPanel northPanel = new JPanel(); JPanel middle = new JPanel(); middle.setLayout(new BoxLayout(middle, BoxLayout.X_AXIS)); middle.add(new JButton(“FOO”)); middle.add(Box.createHorizontalGlue()); JPanel aPanel = new […]

为Graphics2D绘图创建空间

我想绘制一个由Graphics2D矩形组成的简单板,但我也希望在这个板下有一个JButton。 我知道这个板的确切尺寸(以像素为单位),我试图处理getContentPane()方法和BoxLayout,如下所示: frame.getContentPane().add(board); frame.getContentPane().add(Box.createRigidArea(new Dimension(bWidth, bHeight))); frame.getContentPane().add(new JButton(“Start”)); frame.pack(); 但RigidArea并非真正隐形,它会覆盖我的绘图。 能否请您给我一些如何使其正常工作的提示? :(我只想要一个小按钮,它让我现在坐在这里大约2个小时…… 谢谢!

使用BoxLayout动态增长JPanel(在空布局上)

我有一个JPanel,在JPanel的顶部有一个垂直BoxLayout,其布局为null。 我希望带有BoxLayout的JPanel随着组件的添加而增长。 看到这段代码: public static void main (String[] args) { JFrame f = new JFrame(); f.setSize(500,500); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel total = new JPanel(); total.setLayout(null); total.setSize(f.getWidth(),f.getHeight()); total.setBackground(Color.green); JPanel box = new JPanel(); box.setLocation(100,200); box.setLayout(new BoxLayout(box, BoxLayout.Y_AXIS)); box.add(new JButton(“test”)); box.add(new JLabel(“hey”)); total.add(box); f.add(total); f.setVisible(true); } 您会注意到没有任何组件出现。 如何添加更多组件(垂直添加)时,如何使JPanel“框”大小动态增加。 提前:我需要“box”的位置正好是100,200所以请不要建议我不使用null布局。 我必须使用null布局。 “total”的null布局不应影响我的问题的答案,该问题集中在“box”面板上。

我怎样才能在Java中实现这种可扩展的布局? 灵活的BoxLayout等

我希望能够有三个JPanels p1 p2和p3,并让它们像这样布局: 我一直在玩FlowLayout,BoxLayout等,但我不确定我是否朝着正确的方向前进。 我是Java的新手,所以如果我很诚实,我不知道会发生什么。 我喜欢BoxLayout如何工作,调整面板大小,但我希望能够给它一些宽度属性。 我没有使用视觉设计师,这是我目前的窗口代码: private void initialize() { Dimension dimensions = Toolkit.getDefaultToolkit().getScreenSize(); frame = new JFrame(); frame.setLayout(new GridLayout(1, 3)); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setBounds(dimensions.width / 2 – WINDOW_WIDTH / 2, dimensions.height / 2 – WINDOW_HEIGHT / 2, WINDOW_WIDTH, WINDOW_HEIGHT); JPanel p1 = new JPanel(new BorderLayout()); p1.setBackground(Color.red); JPanel p2 = new JPanel(new BorderLayout()); p2.setBackground(Color.black); JPanel p3 […]

如何防止Boxlayout / Box拉伸儿童组件?

正如您在下面的runnable代码中看到的,我尝试使用具有可扩展子框的Box。 儿童盒可以改变它们的大小,这一切都很好。 主要问题是大小始终相对于父级。 但我希望它们具有特定的大小,以防万一没有地方使用JScrollPane。 目前他们只收缩其他儿童盒子。 我尝试了Glue和Filler,但它没有用。 胶水没有任何影响,填料具有副作用,始终保持在某些位置(即使ScrollPane正在运行)。 拥有如此多的免费空间真是难看。 所以,你知道防止盒子伸展孩子的好方法吗? 先感谢您! import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BorderFactory; import javax.swing.Box; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.border.TitledBorder; public class ExpandableMenueDemo { Box allBoxes; ExpandableMenueDemo(){ allBoxes = Box.createVerticalBox(); TitledBorder title; title = BorderFactory.createTitledBorder(“Filter”); allBoxes.setBorder(title); for (int i = 0 […]

BoxLayout忽略setYAlighment

这是一个function: /** * Creates an instance of a JLabel with the given arguments * @param text The text to be displayed on the Label * @param font The font of the label * @param bold set to true if you want the label’s text to be bold * @param fontSize The size of the font […]

java BoxLayout面板的对齐方式

我浏览过,并没有找到专门针对我的情况定制的解决方案。 我有一个面板,我在一个对话框中显示: //create dialog panel JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); panel.add(headerPanel); panel.add(type1Panel); panel.add(type2Panel); panel.add(type3Panel); panel.add(type4Panel); panel.add(type5Panel); panel.add(type6Panel); int result = JOptionPane.showConfirmDialog(null, panel, “Please enter values.”, JOptionPane.OK_CANCEL_OPTION); 最后两个面板的大小,类型5和类型6,大小相同,所以它们看起来很好。 但是,标题和前4个面板的大小不同,我希望它们全部左对齐。 到目前为止,我还没有找到一个很好的解决方案,如何解决这个问题。 问题是,我怎么能保持对齐前5个面板,而不是最后2个? 如果不是,我怎么能将它们全部对齐? setalignmentx()不适用于面板。 我已经尝试过使用GridLayout,但是gui主窗口的宽度相当大,并且不适合屏幕,因此BoxLayout沿Y轴。感谢任何帮助或建议。