gpt4 book ai didi

java - 按钮和窗口之间的空间

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:52:17 25 4
gpt4 key购买 nike

我这里有这段代码,我的想法是在一个主窗口中有一个文本区域旁边的两个按钮,我还没有添加它们。在尝试使用 GridBagLayout 并在此过程中扯掉我的头发后,我决定不使用布局并手动将按钮放置在不可调整大小的窗口中。

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Tema extends JFrame implements ActionListener {

JMenuBar menubar = new JMenuBar();
JMenu actiuni = new JMenu("Actiuni");
JMenu contact = new JMenu("Contact");
JMenu help = new JMenu("Help");
JMenuItem ntest = new JMenuItem("Nou test");
JMenuItem vizarh = new JMenuItem("Vizualizare arhiva");
JMenuItem datcon = new JMenuItem("Date de contact");
JMenuItem sendmail = new JMenuItem("Trimite e-mail");
JMenuItem instrut = new JMenuItem("Instructiuni de utilizare");
JButton b1 = new JButton("Incepe testul!");
JButton b2 = new JButton("Vezi arhiva!");
JTextArea ta = new JTextArea("Default text", 5, 30);

public void common(String s)
{
setSize(800,450);
setLocationRelativeTo(null);
setResizable(false);

setTitle(s);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


menubar.add(actiuni);
menubar.add(contact);
menubar.add(help);
actiuni.add(ntest);
actiuni.add(vizarh);
contact.add(datcon);
contact.add(sendmail);
help.add(instrut);

setJMenuBar(menubar);

}

public Tema()
{
common("Self-Esteem- Fereastra Principala");
JPanel cp = new JPanel();
cp.setLayout(null);


b1.setBounds(100,100,200,100);
cp.add(b1);


b2.setBounds(100,250,200,100);
cp.add(b2);

setContentPane(cp);
setVisible(true);

}

public static void main(String[] args)
{
Tema x = new Tema();
}


@Override
public void actionPerformed (ActionEvent e){

}

}

但是输出是这样的: enter image description here

我的问题是为什么第二个按钮下方的空间不等于第一个按钮上方的空间?它们不应该都是 100 像素吗?

最佳答案

  • 不要在不必要的情况下扩展 JFrame 类。
  • 切勿使用 Absolute/Null LayoutManager。使用适当的 LayoutManager,这包括嵌套布局以获得所需的外观。在这里查看好的教程:
  • 不要在 JFrame 上调用 JFrame#setSize(..) 而只是在设置 之前调用 JFrame#pack() JFrame可见。
  • 不要使用 JFrame#setContentPane(...) 只需在 JFrame 实例上使用 add(..)
  • 创建 Event Dispatch Thread初始化和更改 UI 组件
  • 不要为多个组件实现单个 ActionListener。除非它会被其他类访问或组件共享一个Action。而是使用 Anonymous ActionListener

这是我制作的一个示例(基本上是您的代码已修复)希望对您有所帮助:

enter image description here

import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class LayoutTest {

private final JMenuBar menubar = new JMenuBar();
private final JMenu actiuni = new JMenu("Actiuni");
private final JMenu contact = new JMenu("Contact");
private final JMenu help = new JMenu("Help");
private final JMenuItem ntest = new JMenuItem("Nou test");
private final JMenuItem vizarh = new JMenuItem("Vizualizare arhiva");
private final JMenuItem datcon = new JMenuItem("Date de contact");
private final JMenuItem sendmail = new JMenuItem("Trimite e-mail");
private final JMenuItem instrut = new JMenuItem("Instructiuni de utilizare");
private final JButton b1 = new JButton("Incepe testul!");
private final JButton b2 = new JButton("Vezi arhiva!");
private final JTextArea ta = new JTextArea("Default text", 5, 30);
//create JFrame instance
private final JFrame frame = new JFrame();

public LayoutTest() {
initComponents();
}

public static void main(String[] args) {
//creat UI on EDT
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new LayoutTest();
}
});
}

private void initComponents() {
frame.setTitle("Self-Esteem- Fereastra Principala");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

menubar.add(actiuni);
menubar.add(contact);
menubar.add(help);
actiuni.add(ntest);
actiuni.add(vizarh);
contact.add(datcon);
contact.add(sendmail);
help.add(instrut);

frame.setJMenuBar(menubar);
JPanel textAreaJPanel = new JPanel();

//create button panel with GridLayout(2,1)
JPanel buttonJPanel = new JPanel(new GridLayout(2, 1));//new GridLayout(2, 1,10,10) creates gridlayout with horixontal and vertcial spacing of 10

//add buttons to one panel
buttonJPanel.add(b1);
buttonJPanel.add(b2);
//add text area to textarea jPanel
textAreaJPanel.add(ta);

//add textarea panel to west of content pane (BorderLayout by default)
frame.add(textAreaJPanel, BorderLayout.WEST);

//add button Panel to EAST of JFrame content pane
frame.add(buttonJPanel, BorderLayout.EAST);

frame.pack();

frame.setResizable(false);
frame.setLocationRelativeTo(null);

frame.setVisible(true);
}
}

关于java - 按钮和窗口之间的空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13030800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com