我需要以下代码的帮助。我要做的是扩展TextArea(名为preview
)的大小以包括最后三个按钮:algo1
,algo2
和algo3
。
我已多次尝试更改代码,但它仍然只显示一个按钮,即algo1
,而不是所有三个按钮。它是否与BASELINE
或LEADING
有关?有人可以告诉我哪里出错了吗?感谢。
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();
}
}
答案 0 :(得分:1)
我对Java很新。我如何进行invokeLater包装?任何 例子?感谢
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]
含义
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]
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();
}
});
}
}
答案 1 :(得分:0)
我不知道您要添加按钮的位置,但请尝试此示例。我更新了你的代码,现在你可以看到你的按钮: 这里是按钮:but1 but2 but3
package Core;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
// 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)
.addGroup(
layout.createSequentialGroup().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.BASELINE).addComponent(label3)
.addComponent(preview).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();
}
}
或试试这个: 这是:
BUTT1
BUTT2
butt3
package Core;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
// 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().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();
}
}