我看到它的GridBagLayout
位置是细胞中心对齐的孩子.如何左右对齐?
UPDATE
构建代码(我知道我可以重用c
)
// button panel
JPanel button_panel = new JPanel();
button_panel.add(ok_button);
button_panel.add(cancel_button);
// placing controls to dialog
GridBagConstraints c;
GridBagLayout layout = new GridBagLayout();
setLayout(layout);
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
add(inputSource_label, c);
c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 0;
add(inputSource_combo, c);
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 1;
add(output_label, c);
c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 1;
add(output_combo, c);
c = …
Run Code Online (Sandbox Code Playgroud) 我对理解这两个属性有些困难.我该如何给予组件重量?这些数字是如何计算的?我曾试图在网上阅读几篇文章,但我不明白.
谢谢.
是否可以GridBagLayout
为整行/列设置边距/填充?我在constraints-object上使用了inset,但是,使用这种方法我需要在每个组件上从底部设置填充.
是否可以填充所有内容JFrame
?因为现在每个组件都与框架对齐.
constraints.weightx = 2;
constraints.weighty = 1;
constraints.fill = GridBagConstraints.BOTH;
addComponent(this, layout, constraints, questionPanel = new QuestionPanel(), 0, 0, 1, 1);
constraints.weightx = 1;
constraints.weighty = 1;
constraints.fill = GridBagConstraints.BOTH;
addComponent(this, layout, constraints, categoryPanel = new CategoryPanel(), 0, 1, 1, 1);
constraints.weightx = 1;
constraints.weighty = 0;
constraints.fill = GridBagConstraints.HORIZONTAL;
addComponent(this, layout, constraints, answerPanel = new AnswerPanel(), 1, 0, 2, 1);
constraints.weightx = 1;
constraints.weighty = 2;
constraints.fill = GridBagConstraints.BOTH;
addComponent(this, layout, constraints, tabPane = …
Run Code Online (Sandbox Code Playgroud) 大家好!我正在尝试解决一个很明显的问题,但我无法解决它.我正在使用Java/Swing库开发示例应用程序; 我有一个JFrame和一个JPanel.我只想达到以下目标:
JPanel 必须在JFrame内部居中.
的JPanel 必须有ALWAYS一个与指定的大小
有必要对setPreferredSize()方法.它不能在这个尺寸下调整大小.
我尝试使用GridBagLayout:这是我能做到的唯一方式.
请参阅以下示例:
/* file StackSample01.java */
import java.awt.*;
import javax.swing.*;
public class StackSample01 {
public static void main(String [] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(100, 100));
panel.setBackground(Color.RED);
frame.setLayout(new GridBagLayout());
frame.add(panel, new GridBagConstraints());
frame.setSize(new Dimension(200, 200));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个截图:
我不会使用GridBagLayout做一件太简单的事情.我尝试了一个最简单的解决方案,使用Box,但这不起作用:
示例代码:
/* file StackSample02.java */
import java.awt.*;
import javax.swing.*;
public class StackSample02 {
public static void main(String …
Run Code Online (Sandbox Code Playgroud) 到目前为止,我设法尽可能避免使用GridBagLayout
(手动代码),但这次我无法避免,我正在阅读SUN的教程GridBagLayout
到目前为止它还不顺利.我想我很想念一些事情.
例如,我尝试以下代码(类似于SUN的帖子中的代码):
public class MainFrame extends JFrame {
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainFrame frame = new MainFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame
*/
public MainFrame() {
super();
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container mainContainer = getContentPane();
mainContainer.setLayout(new GridBagLayout());
//add label
JLabel someLabel = new JLabel("Label 1:");
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0; …
Run Code Online (Sandbox Code Playgroud) 是否可以使用GridBagLayout管理器完全模拟GridLayout的行为?
基本上,我有一个8x8网格,其中每个单元格应具有相同的宽度和高度.GridLayout自动执行此操作.但我想在网格中添加另一行和列,其大小与其他大小不同.该行/列应占用可能遗留的所有剩余空间(因为可用大小无法均等地分配到8个单元中).这是可能的,还是我 - 再次 - 必须使用不同的布局管理器?
这是我想要实现的简单图形,简化为4个单元格:
有色单元格是我添加到实际网格(灰色)的单元格,其中单元格具有相同的高度和宽度x
.因此网格的高度和宽度是4*x
.我现在希望其他单元格具有必要的宽度/高度(minimumSize)加上完整大小的可用宽度/高度的其余部分.
如果更改整个面板的大小,灰色网格单元应该再次占用尽可能多的空间.
嗨,先谢谢你的任何帮助,我正在尝试构建一个简单的程序来学习GUI,但是当我运行JTextFields下面的代码时,所有显示的切口都不够大,即使是一个角色.
无法发布图像,但它看起来类似于:标签[|
哪里[| 文本字段实际上是什么样子
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class lab6start implements ActionListener
{
JTextField custNameTxt;
JTextField acctNumTxt;
JTextField dateCreatedTxt;
JButton checkingBtn;
JButton savingsBtn;
JTextField witAmountTxt;
JButton withDrawBtn;
JTextField depAmountTxt;
JButton depositBtn;
lab6start()
{
JFrame bankTeller = new JFrame("Welcome to Suchnsuch Bank");
bankTeller.setSize(500, 280);
bankTeller.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
bankTeller.setResizable(false);
bankTeller.setLayout(new GridBagLayout());
bankTeller.setBackground(Color.gray);
//bankTeller.getContentPane().add(everything, BorderLayout.CENTER);
GridBagConstraints c = new GridBagConstraints();
JPanel acctInfo = new JPanel(new GridBagLayout());
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 2;
c.gridheight = 1;
c.insets = …
Run Code Online (Sandbox Code Playgroud) 我是Java Swing的新手,我一直在努力从左上角开始GridBagLayout,这样c.gridx = 0 c.gridy = 0会将我的对象放在左上角.
如果你能在这一点之后告诉我需要做什么,我将不胜感激:
JPanel panel = new JPanel(new GridBagLayout());
frame.add(panel);
GridBagConstraints c = new GridBagConstraints();
Run Code Online (Sandbox Code Playgroud)
我知道我必须使用NORTHWEST或FIRST_LINE_START常量,但我不知道如何.我尝试这样做'但它没有意识到常数.
frame.getContentPane().add(panel, BorderLayout.NORTHWEST);
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助.
在下面的GridBagLayout代码中,我希望在调整JFrame的大小以使其更小时,可以遵守JButton btn2的指定最小大小.但是当我使JFrame变小时,btn2会小于其最小尺寸然后消失.
有人能指出我正在做错的方向吗?也许我必须设置包含按钮的JPanel的最小大小?
任何帮助表示赞赏,谢谢!
JFrame frame = new JFrame();
frame.setSize(400,300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setMinimumSize(new Dimension(400,300));
panel.setBackground(Color.RED);
panel.setLayout(new GridBagLayout());
GridBagConstraints gbc = null;
JButton btn1 = new JButton("btn1");
btn1.setPreferredSize(new Dimension(150,50));
btn1.setMinimumSize(new Dimension(150,50));
gbc = new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,
new Insets(0,0,0,0), 0, 0);
panel.add(btn1, gbc);
JButton btn2 = new JButton("btn2");
btn2.setPreferredSize(new Dimension(150,150));
btn2.setMinimumSize(new Dimension(150,150));
gbc = new GridBagConstraints(1, 0, 1, 1, 1.0, 1.0,
GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH,
new Insets(0,0,100,100), 0, 0);
panel.add(btn2, gbc);
frame.getContentPane().add(panel);
frame.setVisible(true);
Run Code Online (Sandbox Code Playgroud) 我有一个JMenuItem
用ActionListener
,在此ActionListener
我要一个添加GridBagLayout
到我的frame
(这可能会或可能不会有被添加到内容窗格-用于测试目的,并不),然后添加components
到frame
.frame works
它本身的设计,但我希望trigger
它来自ActionListener
一个JMenuItem
,这里是我遇到问题的地方.它不会从内部显示ActionListener
.我已尝试从AL中的类中的不同方法运行相同的代码,但这也不起作用.
当我ActionListener
完全注释掉时,JLabel
我想测试添加到GBL
正确的位置,系统prints
我的debug
线路在这里和here2.没有语法错误compiler
.这产生了期望的结果,并且打印了标签.(请参阅下面的图片,了解当我完全注释掉AL时会发生什么.)有问题的代码片段(在哪个帧中是我的JFrame
)如下:
// (frame created, menus added, etc.) ...
JMenuItem vPoke1Item = new JMenuItem("Pokemon 1");
vPoke1Item.setActionCommand("poke1");
viewMenu.add(vPoke1Item);
//Setup GBL to view stats for Pokemon 1
vPoke1Item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
// debug output
System.out.println("here"); …
Run Code Online (Sandbox Code Playgroud)