简体   繁体   English

如何使秋千部件尺寸不同

[英]How to make swing components different sizes

How to make specific sizes for Swing components in java? 如何在Java中为Swing组件设置特定大小? The gui is supposed to look like this... gui应该看起来像这样...

+++++++++++++++++
+  TextField    +
+++++++++++++++++
+               +
+  TextPane     +
+               +
+++++++++++++++++
+Button +Button +
+++++++++++++++++

I've tried BorderLayout , GridLayout , nested JPanels , etc, But I can't get a good looking gui where the JButtons aren't like bricks and the JTextField isn't three times the size of the text. 我已经尝试过BorderLayoutGridLayout ,嵌套的JPanels等,但是在JButtons不像砖块且JTextField不是文本大小的三倍的情况下,我却无法获得美观的GUI。

Part of it depends on how you want space extra assigned. 部分取决于您要如何额外分配空间。 This GUI provides extra width to the text field and text area, while keeping the buttons centered. 该GUI为文本字段和文本区域提供了额外的宽度,同时使按钮居中。 Extra height is given to the text area. 额外的高度被赋予文本区域。

基本布局

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.*;

public class BasicLayout {

    BasicLayout() {
        JPanel gui = new JPanel(new BorderLayout(2,2));

        gui.add(new JTextField(), BorderLayout.PAGE_START);
        gui.add(new JTextArea(3,15));

        JPanel controls = new JPanel(new FlowLayout(FlowLayout.CENTER,5,5));
        controls.add(new JButton("Button1"));
        controls.add(new JButton("Button2"));

        gui.add(controls, BorderLayout.PAGE_END);

        JOptionPane.showMessageDialog(null, gui);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new BasicLayout();
            }
        });
    }
}

Update 更新资料

At first I thought you meant JTextArea in the middle part, then I looked more closely. 起初我以为您的意思是中间的JTextArea ,然后我仔细看了一下。 A JTextPane is slightly more tricky to size because it does not accept size hints in the constructor. JTextPane大小稍微有些棘手,因为它在构造函数中不接受大小提示。 For that we can tweak the preferred size of the container for it (in this case, a JScrollPane . 为此,我们可以为其调整容器的首选大小(在本例中为JScrollPane

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.*;

public class BasicLayout {

    BasicLayout() {
        JPanel gui = new JPanel(new BorderLayout(2,2));

        gui.add(new JTextField(), BorderLayout.PAGE_START);
        JTextPane text = new JTextPane();
        JScrollPane scroll = new JScrollPane(text);
        Dimension d = text.getPreferredSize();
        scroll.setPreferredSize(new Dimension(d.width, d.height*3));
        gui.add(scroll);

        JPanel controls = new JPanel(new FlowLayout(FlowLayout.CENTER,5,5));
        controls.add(new JButton("Button1"));
        controls.add(new JButton("Button2"));

        gui.add(controls, BorderLayout.PAGE_END);
        JOptionPane.showMessageDialog(null, gui);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new BasicLayout();
            }
        });
    }
}

A LayoutManager is used to simplify specific layout. LayoutManager用于简化特定的布局。 in your case it seems that you want to do something specific, so don't use a layout manager! 在您的情况下,您似乎想做一些特定的事情,所以不要使用布局管理器!

On the oracle website there is an great guide to layouts. 在oracle网站上,有很好的布局指南。 One part of this guide is how to not use it : http://docs.oracle.com/javase/tutorial/uiswing/layout/none.html 本指南的一部分是如何不使用它: http : //docs.oracle.com/javase/tutorial/uiswing/layout/none.html

Good Luck with your project! 祝您项目顺利!

If you are looking for designing a GUI in Netbeans, it is possible. 如果您要在Netbeans中设计GUI,则可以。 Once you have created a Java project, select the project and goto: 创建Java项目后,选择该项目并转到:

File > New File

Then the dialog will appear (that you already know). 然后将出现对话框(您已经知道)。

In categories, select Swing GUI Forms and then select whatever you want from the options 在类别中,选择“ Swing GUI Forms ,然后从选项中选择所需的任何内容

Here is a screenshot 这是截图 在此处输入图片说明

The best way to Do that is to use the .setBounds(X, Y, Length, Width) You can set exact positioning of item. 最好的方法是使用.setBounds(X,Y,Length,Width)您可以设置项目的精确位置。 It is measured from the top left corner for the X and the Y. For the width it grow what ever the time is to the left and dose not move the X. The same goes with the length and the Y values. 它是从X和Y的左上角开始测量的。宽度随着时间的增长而向左移动,并且不会移动X。长度和Y值也是如此。

JTextField field = new JTextField(),
field.setBounds(500, 100, 100, 30);
add(field);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM