gpt4 book ai didi

Java Swing GroupLayout 不包括预期的 3 个按钮。如何解决这个问题?

转载 作者:搜寻专家 更新时间:2023-11-01 03:40:27 28 4
gpt4 key购买 nike

我需要一些关于下面代码的帮助。我想要做的是扩大 TextArea(名为 preview)的大小以包括最后三个按钮:algo1algo2algo3.

我已经多次尝试更改代码,但它仍然只显示一个按钮,即 algo1,而不是所有三个按钮。它与 BASELINELEADING 有关系吗?有人可以告诉我哪里出错了吗?谢谢。

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

// Create a simple GUI window
public class win {

private static void createWindow() {

//Create and set up the window.
JFrame frame = new JFrame("PDF Denoiser");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//My edit
JPanel panel = new JPanel();
GroupLayout layout = new GroupLayout(panel);
panel.setLayout(layout);

layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);

JLabel label1 = new JLabel("Image File");
JLabel label2 = new JLabel("Destination");
JLabel label3 = new JLabel("Preview");

JTextField current = new JTextField();
JTextField dest = new JTextField();
JTextArea preview = new JTextArea();

JButton choose1 = new JButton("Search1");
JButton choose2 = new JButton("Search2");
JButton algo1 = new JButton("MDWM");
JButton algo2 = new JButton("BFMR");
JButton algo3 = new JButton("Mine");

//Horizontal arrangement
layout.setHorizontalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(label1)
.addComponent(label2)
.addComponent(label3))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(current)
.addComponent(dest)
.addComponent(preview))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(choose1)
.addComponent(choose2)
.addComponent(algo1)
.addComponent(algo2)
.addComponent(algo3))
);

layout.linkSize(SwingConstants.HORIZONTAL, choose1, choose2, algo1, algo2, algo3);

//Vertical arrangement
layout.setVerticalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(label1)
.addComponent(current)
.addComponent(choose1))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(label2)
.addComponent(dest)
.addComponent(choose2))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(label3)
.addComponent(preview)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(algo1)
.addComponent(algo2)
.addComponent(algo3))))
);






//Display the window.
frame.setLocationRelativeTo(null);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {

createWindow();

}

}

最佳答案

I'm quite new to Java. How do I do the invokeLater wrap? Any examples? Thanks

run:
java.awt.Rectangle[x=208,y=12,width=82,height=26]
java.awt.Rectangle[x=208,y=12,width=82,height=26]
java.awt.Rectangle[x=208,y=76,width=82,height=26]
java.awt.Rectangle[x=208,y=76,width=82,height=26]
java.awt.Rectangle[x=208,y=76,width=82,height=26]
  • 看到所有三个 JButton 在屏幕上都获得了相同的坐标

意义

System.out.println(algo1.getBounds());
System.out.println(algo2.getBounds());
System.out.println(algo3.getBounds());

返回

java.awt.Rectangle[x=208,y=76,width=82,height=26]
java.awt.Rectangle[x=208,y=76,width=82,height=26]
java.awt.Rectangle[x=208,y=76,width=82,height=26]

enter image description here

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

// Create a simple GUI window
public class Win {

private static void createWindow() {
//Create and set up the window.
JFrame frame = new JFrame("PDF Denoiser");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//My edit
JPanel panel = new JPanel();
GroupLayout layout = new GroupLayout(panel);
panel.setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
JLabel label1 = new JLabel("Image File");
JLabel label2 = new JLabel("Destination");
JLabel label3 = new JLabel("Preview");
JTextField current = new JTextField(10);
JTextField dest = new JTextField(10);
JTextArea preview = new JTextArea(5, 10);
final JButton choose1 = new JButton("Search1");
//choose1.setPreferredSize(new Dimension(80,20));
final JButton choose2 = new JButton("Search2");
//choose2.setPreferredSize(new Dimension(80,20));
final JButton algo1 = new JButton("MDWM");
final JButton algo2 = new JButton("BFMR");
final JButton algo3 = new JButton("Mine");
//Horizontal arrangement
layout.setHorizontalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(label1)
.addComponent(label2)
.addComponent(label3))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(current)
.addComponent(dest)
.addComponent(preview))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(choose1)
.addComponent(choose2)
.addComponent(algo1)
.addComponent(algo2)
.addComponent(algo3)));
layout.linkSize(SwingConstants.HORIZONTAL, choose1, choose2, algo1, algo2, algo3);
//Vertical arrangement
layout.setVerticalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(label1)
.addComponent(current)
.addComponent(choose1))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(label2)
.addComponent(dest)
.addComponent(choose2))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(label3)
.addComponent(preview)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(algo1)
.addComponent(algo2)
.addComponent(algo3)))));
//Display the window.
frame.setLocationRelativeTo(null);
frame.add(panel);
frame.pack();
frame.setVisible(true);
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
System.out.println(choose1.getBounds());
System.out.println(choose1.getBounds());
System.out.println(algo1.getBounds());
System.out.println(algo2.getBounds());
System.out.println(algo3.getBounds());
}
});
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
createWindow();
}
});
}
}
  • 故事结束

关于Java Swing GroupLayout 不包括预期的 3 个按钮。如何解决这个问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15873523/

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