首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java MigLayout问题

Java MigLayout问题
EN

Stack Overflow用户
提问于 2011-04-22 08:51:16
回答 1查看 975关注 0票数 0

我正在尝试使用migLayout构建一个计算器图形用户界面,但我不熟悉这种布局。

我的问题是我的GUI是一条直线的按钮。

代码语言:javascript
复制
1 2 3 + 4 5 6 - ... etc

I would like to get
1 2 3 +
4 5 6 -
7 8 etc...

import net.miginfocom.swing.MigLayout;
import javax.swing.*;
import java.awt.*;

public class Calculator1 {

    public static void main(String args[]) {
        JFrame frame = new JFrame("Calculator1");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new MigLayout());

        frame.add(new JTextField("                                               "),"wrap");
        frame.add(new JButton("1"));
        frame.add(new JButton("2"));
        frame.add(new JButton("3"));
        frame.add(new JButton("+"));
        frame.add(new JButton("4"));
        frame.add(new JButton("5"));
        frame.add(new JButton("6"));
        frame.add(new JButton("-"));
        frame.add(new JButton("7"));
        frame.add(new JButton("8"));
        frame.add(new JButton("9"));
        frame.add(new JButton("*"));
        frame.add(new JButton("0"));
        frame.add(new JButton("/"));
        frame.add(new JButton("="));

        frame.pack();
        frame.setVisible(true);
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-04-22 09:09:17

试试这个:

代码语言:javascript
复制
import net.miginfocom.swing.MigLayout;
import javax.swing.*;
import java.awt.*;

public class Calculator1 {

    public static void main(String args[]) {
        JFrame frame = new JFrame("Calculator1");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new MigLayout("fill, wrap 4", "[25%][25%][25%][25%]", "[shrink]"));

        frame.add(new JTextField("                                               "),"span 4, wrap");
        frame.add(new JButton("1"));
        frame.add(new JButton("2"));
        frame.add(new JButton("3"));
        frame.add(new JButton("+"));
        frame.add(new JButton("4"));
        frame.add(new JButton("5"));
        frame.add(new JButton("6"));
        frame.add(new JButton("-"));
        frame.add(new JButton("7"));
        frame.add(new JButton("8"));
        frame.add(new JButton("9"));
        frame.add(new JButton("*"));
        frame.add(new JButton("0"));
        frame.add(new JButton("/"));
        frame.add(new JButton("="));

        frame.pack();
        frame.setVisible(true);
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5751301

复制
相关文章

相似问题

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