简体   繁体   English

摆件的尺寸和位置出现问题

[英]trouble with size and positions of components in swing

Im making a simple java swing program but i'm having trouble with the size and positions of my components. 我制作了一个简单的java swing程序,但是我在组件的大小和位置上遇到了麻烦。 I have three Jlabels and three JtextFields. 我有三个Jlabels和三个JtextFields。 The jlabels are overlapping so only the last one i add is showing. jlabel是重叠的,因此仅显示我添加的最后一个。 The textfields are showing up very small.(almost look like lines.) Also, everything appears on one line and i would like each component on its own line. 文本字段显示的很小。(几乎看起来像是行。)而且,所有内容都显示在一行上,我希望每个组件都在自己的行上。

CODE: 码:

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class setCustoms {
static JPanel panel = new JPanel();
static JFrame frame2 = new JFrame("Settings");

public static void makeWindow(){
    frame2.setVisible(true);
    frame2.add(panel);
    frame2.setBounds(500, 300, 500, 200);

    JLabel app1l = new JLabel("");
    JLabel app2l = new JLabel("");
    JLabel app3l = new JLabel("");
    JLabel site1l = new JLabel("");
    JLabel site2l = new JLabel("");
    JLabel site3l = new JLabel("");

    panel.add(app1l);
    app1l.setText("Set  Application ONE name and path");
    JTextField app1t = new JTextField();
    app1t.setBounds(100, 100, 100, 25);
    app1t.addActionListener(new Action());
    panel.add(app1t);

    panel.add(app2l);
    app1l.setText("Set  Application TWO name and path");
    JTextField app2t = new JTextField();
    app2t.addActionListener(new Action());
    panel.add(app2t);


    panel.add(app3l);
    app1l.setText("Set  Application THREE name and path");
    JTextField app3t = new JTextField();
    app3t.addActionListener(new Action());
    panel.add(app3t);

}
static class Action implements ActionListener{
    public void actionPerformed (ActionEvent e){


    }
}

}

EDIT: althoguh i do have code to set bounds for certain items, it does not work 编辑:althoguh我确实有代码来设置某些项目的范围,它不起作用

The JPanel uses a FlowLayout by default, this lays out each component next to each in a line. JPanel默认使用FlowLayout ,这会将每个组件FlowLayout在一行中的每个组件旁边。

setBounds has no effect as the panel is under the control of a layout manager, which is making it's own decisions about how best to place and size the components setBounds无效,因为面板受布局管理器的控制,而布局管理器由自己决定如何最佳放置和调整组件大小

Try using a different layout manager. 尝试使用其他布局管理器。

Take a look at Laying out components within a container for more details and ideas 看一下在容器布置组件以了解更多详细信息和想法

You need to use a LayoutManager (A Visual Guide to Layout Managers) to organize your components. 您需要使用LayoutManager(布局管理器的可视指南)来组织组件。

A example with GridLayout: GridLayout的示例:

1) Configure GridLayout in your JFrame 1)在JFrame中配置GridLayout

// with 3 rows and 2 columns
static JPanel panel = new JPanel(new GridLayout(3, 2));

2) Create and add the elements in order left-to-right-to-down 2)按从左到右从下到下的顺序创建和添加元素

JLabel app1l = new JLabel("");
app1l.setText("Set  Application ONE name and path");
panel.add(app1l);

JTextField app1t = new JTextField();
panel.add(app1t);

// (...)
//keep adding your elements in order

3) Pack your JFrame to organize all elements added 3)打包您的JFrame以整理所有添加的元素

frame3.pack(); 
  • Everything appears on one line and i would like each component on its own line. 一切都出现在一行上,我希望每个组件都在自己的行上。

As MadProgrammer pointed out, the elements appear in a single row because you're not setting the layout manager for panel . 正如MadProgrammer指出的那样,这些元素显示在一行中,因为您没有为panel设置布局管理器。 Following Pedro Vítor's suggestion, you could use a grid layout . 按照PedroVítor的建议,您可以使用网格布局 For example, for a single-column layout you can do: 例如,对于单列布局,您可以执行以下操作:

static JPanel panel = new JPanel (new GridLayout (0,1));

where 0 indicates no rows and 1 is the number of columns. 其中0表示没有行,而1表示列数。

  • The jlabels are overlapping jlabel重叠

Take a closer look at your code. 仔细看看您的代码。 What's actually happenning is that you're setting the text only for app1l all the time. 实际发生的是,您一直都在只为app1l设置文本。 The other labels are there. 其他标签在那里。 It's just that they have no text. 只是他们没有文字。 Probably the product of copy and paste ;) 可能是复制粘贴的产品;)

  • A note on practices 惯例说明

As Pedro Vítor mentioned, you should make a habit of adding elements in order. 正如PedroVítor所述,您应该养成按顺序添加元素的习惯。 It'll save you headaches when you go back to the code. 回到代码时,这将避免您的头痛。 Additionally, I'd advise you to make the frame visible only after you've added ALL the components . 此外,我建议您仅在添加所有组件之后才使框架可见。 Otherwise, it is not guaranteed that all those components will be rendered. 否则,不能保证将渲染所有这些组件。 Take a look at this question: Why shouldn't I call setVisible(true) before adding components? 看一下这个问题: 为什么在添加组件之前不应该调用setVisible(true)?

So, taking all into account, your code should look something like this: 因此,综合考虑,您的代码应如下所示:

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class setCustoms {
static JPanel panel = new JPanel(new GridLayout(0,1));
static JFrame frame2 = new JFrame("Settings");

public static void main (String[] args){
    JLabel app1l = new JLabel("");
    JLabel app2l = new JLabel("");
    JLabel app3l = new JLabel("");

    app1l.setText("Set  Application ONE name and path");
    panel.add(app1l);
    JTextField app1t = new JTextField();
    app1t.setBounds(100, 100, 100, 25);
    app1t.addActionListener(new Action());
    panel.add(app1t);

    app2l.setText("Set  Application TWO name and path");
    panel.add(app2l);
    JTextField app2t = new JTextField();
    app2t.addActionListener(new Action());
    panel.add(app2t);

    app3l.setText("Set  Application THREE name and path");
    panel.add(app3l);
    JTextField app3t = new JTextField();
    app3t.addActionListener(new Action());
    panel.add(app3t);

    frame2.add(panel);
    frame2.setBounds(500, 300, 500, 200);
    frame2.setVisible(true);   
}

static class Action implements ActionListener{
    public void actionPerformed (ActionEvent e){


    }
}

}

Hope that helps. 希望能有所帮助。

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

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