我正在构建一个PropertyPanel.目前我正在使用a GridLayout来管理JLabels我可以指定值的相应字段.但问题是GridLayout自动管理列的大小:它使它们具有相同的宽度.
这意味着当我有一个很大的值字段时,colum会越来越大(这很好),但是另一列(我所有的JLabels)也越来越大了.这是一个截图:
<坏
正如你所看到的,该image属性具有巨大的价值,这使得两个列更大,并且我在JLabels 之后有很多空间.
所以,我正在寻找一个LayoutManager使每列尽可能大的列.
我想要一个像这样的布局(用Gimp编辑):
<好的
谢谢
我很想得到一个如何让这种粘合剂起作用的演示; 我一直试图让它发挥作用,没有任何反应......
一个很好的例子是实现的类CenteringPanel的:它是所有得到一个JComponent和中心它,离开它未拉伸上一个窗口的中心...我试图编码类似的东西:
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JPanel;
public class CenteringPanel extends JPanel{
private static final long serialVersionUID = 1L;
public CenteringPanel(JComponent toCenter) {
setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
add(Box.createHorizontalGlue());
add(Box.createVerticalGlue());
add(toCenter);
add(Box.createVerticalGlue());
add(Box.createHorizontalGlue());
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个简单的Miglayout对话框,其中的行包含JLabel和JTextFields.根据情况设定许多组件的可见性.但是,当它们被隐藏时,行高度保持不变,留下空白区域.
我已将最小行高设置为0px,并将其他行设置为"增长",但这似乎没有帮助.当然,如果一行可以是0px高并且其内容是隐藏的,那么它应该缩小它不应该吗?显然我错过了什么!
任何帮助非常感谢.
是否有一个用于Swing的LayoutManager在Android中充当LinearLayout?我非常喜欢组件重量的概念.
我希望在使用 MigLayout 到达屏幕的“边缘”时包装 JPanel。目前我有一个 JScrollPane(我只想启用它)。JScrollPane 包含任意数量的水平排列的 JPanel - 当添加面板时,jpanel 会脱离边缘,我希望它添加到下一行。这可能吗?
这是代码:
public void setupPanels(){
JScrollPane scrollPane = new JScrollPane();
JPanel mainPanel = new JPanel(new MigLayout("insets 2"));
for (Object object : objects){
JPanel subPanel = new JPanel(new MigLayout("insets 0"));
mainPanel.add(subPanel, "alignx left, gapx 2px 5px, gapy 2px 2px, top");
}
scrollPane.setViewportView(mainPanel);
}
Run Code Online (Sandbox Code Playgroud)
另外,要添加一个额外的因素,每次到达边缘时我都需要添加一个新的/不同的面板(时间线)-那么有没有办法找出它何时会换行?
谢谢,
我已将org-jdesktop-layout.jar添加到我的项目中的netbeans中的库中.我发现这个.jar文件:C:\Program Files\NetBeans 7.0.1\platform\modules
但是,仍然无法识别以下代码行:
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
Run Code Online (Sandbox Code Playgroud)
我想我错过了正确的导入声明.什么是正确的进口声明?如果有任何其他有用的信息,请告诉我!什么决定了import语句的前缀?
编辑:以下代码似乎来自较旧的GUI库.我有两个选择,转换为更新的GUI标准或访问旧的GUI标准?我之前从未做过这样的事情.我能做什么?
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
.add(jButton1)
.add(layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jLabel1)
.add(jLabel2))
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(capFormattedTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 69, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(wtFormattedTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 69, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))))
.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
Run Code Online (Sandbox Code Playgroud)
我想这可能是一个较旧的图书馆......提前谢谢
我想要一个FlowLayoutforMyPanel来添加一些任意按钮。我添加MyPanel到JFramenorth(JFrame有一个BorderLayout)。我的问题是:
当里面的按钮MyPanel占据超过一行时,它们不会显示。
换句话说,preferredSizeofMyPanel不会自动改变以显示其全部内容。它只显示一排按钮。
public class NewMain {
public static void main(String[] args) {
JFrame mainFrame = new JFrame();
mainFrame.getContentPane().setLayout(new BorderLayout());
mainFrame.setSize(new Dimension(500,500));
JPanel p1 = new MyPanel();
p1.setLayout(new FlowLayout());
mainFrame.add(p1,BorderLayout.NORTH);
mainFrame.add(new JButton("center"),BorderLayout.CENTER);
p1.add(new JButton("helllloo1"));
p1.add(new JButton("helllloo2"));
p1.add(new JButton("helllloo3"));
p1.add(new JButton("helllloo4"));
p1.add(new JButton("helllloo5"));
p1.add(new JButton("helllloo6"));
p1.add(new JButton("helllloo7"));
mainFrame.setVisible(true);
}
public static class MyPanel extends JPanel{
}
}
Run Code Online (Sandbox Code Playgroud)
当我进行更改MyPanel以覆盖getPreferredSize并设置恒定大小时,它可以正常工作。
public static class …Run Code Online (Sandbox Code Playgroud) 所以我要做的是创建这个: 
我正在使用一个网格布局,这是我到目前为止所拥有的:
public class board {
public static void addComponentsToPane(Container pane) {
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JPanel leftTop = new JPanel();
leftTop.setPreferredSize(new Dimension(251,300));
leftTop.setBackground(Color.black);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
pane.add(leftTop, c);
JPanel middleTop = new JPanel();
middleTop.setPreferredSize(new Dimension(251,200));
middleTop.setBackground(Color.green);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 0;
pane.add(middleTop, c);
JPanel rightTop = new JPanel();
rightTop.setPreferredSize(new Dimension(251,600));
rightTop.setBackground(Color.blue);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 2;
c.gridy = 0;
pane.add(rightTop, c);
JPanel leftBottom = new …Run Code Online (Sandbox Code Playgroud) 我JPanel在一个JFrame. 其中一个面板包含一个JTextArea. 目前我这样创建:
JTextArea = new JTextArea(5, 40);
Run Code Online (Sandbox Code Playgroud)
这给了我一个 5 行乘(大约)40 列的文本区域。
这在垂直方向上按我的意愿工作,该区域填充了父容器的整个高度 - 可能是因为父容器是该行中唯一的元素。
水平方向上,父宽度由下面的元素决定,并且它(通常)比实际宽JTextArea。所以我最终得到了一个两边都有大边距的文本区域。更糟糕的是,当我将框架缩小到文本区域与父容器的宽度完全相同时,它突然“轻弹”并变为 1 行高的文本区域,然后是父容器的宽度.
请原谅下面的粗略图,它希望能说明这个问题。
简而言之:如何创建一个JTextArea始终填满可用空间的最大空间?(如果可能的话,如果用户将框架调整得更小,则出现滚动条的最小宽度)
我一直在学习 Java Swing 中的布局,目前正在制作一个简单的计算器。我将使用文本字段进行输入/输出,我想将其拉伸到 2 列(我总共使用了三列),但是当我尝试拉伸它时,它会调整第一列中项目的大小。这是代码和屏幕截图。
应用截图:
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import javax.swing.*;
public class Calculator extends JFrame{
JButton but1, but2, but3, but4, but5, but6,
but7, but8, but9, but0, butPlus, butMinus,
clearAll;
JTextField textResult;
int num1, num2;
public static void main(String[] args){
new Calculator();
}
public Calculator(){
this.setSize(400,400);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Calculator");
JPanel thePanel = new JPanel();
thePanel.setLayout(new GridBagLayout());
GridBagConstraints gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 0;
gridConstraints.gridy = 0;
gridConstraints.gridwidth = 1;
gridConstraints.gridheight = …Run Code Online (Sandbox Code Playgroud)