首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使按钮扩展Java

如何使按钮扩展Java
EN

Stack Overflow用户
提问于 2012-04-05 05:00:53
回答 2查看 1.6K关注 0票数 1

我正在尝试创建一个Java Applet,我在Java网站上四处寻找,发现了一个我编辑过的很好的例子。

在本例中有3个按钮,一个类似于页眉,一个类似于页脚,另一个类似于正文。我的问题是,中间(正文)按钮不会完全展开。

如果你运行这段代码,你就会明白我的意思。

代码语言:javascript
复制
import java.awt.*;
import javax.swing.JButton;
import javax.swing.JFrame;

public class main {
    final static boolean shouldFill = true;
    final static boolean shouldWeightX = true;
    final static boolean RIGHT_TO_LEFT = false;

    public static void addComponentsToPane(Container pane) {
        if (RIGHT_TO_LEFT) {
            pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        }

        JButton button;
    pane.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    if (shouldFill) {
    //natural height, maximum width
    c.fill = GridBagConstraints.HORIZONTAL;
    }


    button = new JButton("hi");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.gridx = 1;
    c.gridy = 0;
    c.ipady = 40;
    pane.add(button, c);

    button = new JButton("Hello, i will not expand");
    c.fill = GridBagConstraints.BOTH;
    c.weighty = 1.0;   //request any extra vertical space
    c.weightx=1.0;
    c.anchor = GridBagConstraints.CENTER; //bottom of space
    c.insets = new Insets(10,0,0,0);  //top padding
    c.gridx = 1;       //aligned with button 2
    c.gridy = 1;       //third row
    pane.add(button, c);


    button = new JButton("fr");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipady = 0;       //reset to default
    c.weighty = 1.0;   //request any extra vertical space
    c.anchor = GridBagConstraints.PAGE_END; //bottom of space
    c.insets = new Insets(10,0,0,0);  //top padding
    c.gridx = 1;       //aligned with button 2
    c.gridwidth = 2;   //2 columns wide
    c.gridy = 2;       //third row
    pane.add(button, c);
    }

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("main");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Set up the content pane.
        addComponentsToPane(frame.getContentPane());

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-04-05 05:27:10

看起来这不是中间按钮的问题,而是底部按钮没有展开,如果你将底部按钮设置为so c.fill = GridBagConstraints.BOTH;,那么它会占用下面的空白空间。

你想让中间再大一点吗?您可能希望将底部按钮的权重更改为.5 c.weighty = .5; //request any extra vertical space

看看这是不是你要找的?

如果你希望中间比顶部和底部大得多,你可能需要创建更多的网格空间,所以不是1,1,1,而是1,3,1,等等

票数 3
EN

Stack Overflow用户

发布于 2012-04-05 05:30:03

我在

网站上找了看,发现了一个很好的例子,我编辑了一下。

所以我想你不用用GridBagLayout了吧?

使用BorderLayout,使位置为BorderLayout.CENTER的可展开按钮。

http://docs.oracle.com/javase/tutorial/uiswing/layout/border.html

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10019181

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档