Java GUI变得不整洁
这是我第一次来这里。Java GUI变得不整洁,java,swing,user-interface,Java,Swing,User Interface,这是我第一次来这里。 我正在编写一个GUI驱动的程序,它允许我对.txt文件执行Caesar的密码。 然而,在我可以添加ActionListeners和ChangeListeners之前,我决定测试GUI。以下是我得到的: 代码如下: package implementation; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class
我正在编写一个GUI驱动的程序,它允许我对
.txt
文件执行Caesar的密码。然而,在我可以添加
ActionListener
s和ChangeListener
s之前,我决定测试GUI。以下是我得到的:
代码如下:
package implementation;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Frame extends JFrame{
public Frame(){
super("Caesar[E]");
this.setVisible(true);
this.setLocationRelativeTo(null);
/*Adding the options to GUI*/
factor.setPreferredSize(new Dimension(30,30));
JToolBar toolbar = new JToolBar();
radio.add(encrypt);
radio.add(decrypt);
toolbar.add(encrypt);
toolbar.add(decrypt);
toolbar.add(factor);
toolbar.setFloatable(false);
/*Adding the JTextArea for input*/
Box inputBound = Box.createHorizontalBox();
Box inputBound_text = Box.createVerticalBox();
Box inputBound_buttons = Box.createVerticalBox();
inputScroll.add(input);
inputScroll.setEnabled(true);
input.setEditable(true);
inputScroll.setBorder(BorderFactory.createTitledBorder("Text/File for Encryption/" +
"Decryption"));
inputBound_text.add(inputScroll);
inputBound_buttons.add(openFile);
inputBound_buttons.add(cancelFileInput);
inputBound.add(inputBound_text);
inputBound.add(Box.createHorizontalStrut(25));
inputBound.add(inputBound_buttons);
/*Adding JTextArea for output*/
Box outputBound = Box.createHorizontalBox();
Box outputBound_text = Box.createVerticalBox();
Box outputBound_buttons = Box.createVerticalBox();
outputScroll.add(output);
output.setEditable(true);
outputScroll.setBorder(BorderFactory.createTitledBorder("Text After Encryption" +
"/Decryption"));
outputBound_text.add(outputScroll);
outputBound_buttons.add(saveFile);
outputBound_buttons.add(send);
outputBound.add(outputBound_text);
outputBound.add(Box.createHorizontalStrut(25));
outputBound.add(outputBound_buttons);
outputBound.setSize(150, 200);
/*Adding JButton for performing the action*/
this.add(performAction,BorderLayout.SOUTH);
/*Adding the components to the Frame*/
Box outerBox = Box.createVerticalBox();
outerBox.add(toolbar,BorderLayout.NORTH);
outerBox.add(inputBound);
outerBox.add(outputBound);
this.add(outerBox);
this.setSize(500, 700);
}
boolean isFileInput = false;
boolean isEncrypt = true;
JButton performAction = new JButton("Encrypt!");
JButton openFile = new JButton("Open a File");
JButton cancelFileInput = new JButton("Cancel File Input");
JButton saveFile = new JButton("Save File");
JButton send = new JButton("Send");
JTextArea input = new JTextArea();
JTextArea output = new JTextArea();
JFileChooser chooser = new JFileChooser();
JScrollPane inputScroll = new JScrollPane();
JScrollPane outputScroll = new JScrollPane();
ButtonGroup radio = new ButtonGroup();
JRadioButton encrypt = new JRadioButton("Encrypt",true);
JRadioButton decrypt = new JRadioButton("Decrypt",false);
JSpinner factor = new JSpinner(new SpinnerNumberModel(1,1,26,1));
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
new Frame();
}
});
}
}
您能告诉我如何解决图中所示的问题吗?setPreferredSize()
,但如何确保我输入的维度正确无误李>我建议你担任布局经理。这样的事情在MiGLayout中很容易我推荐作为LayoutManager。像这样的事情在MiGLayout中很容易试错从来都不是获得所需布局的好方法。相反,使用JTextArea
构造函数,它可以告诉您需要多少行和列
JTextArea(int rows, int columns)
JTextArea
将在pack()
窗口时计算一个好的首选大小,而不需要setSize()
编辑:您说,“JTextArea
处于非活动状态。我无法在其中输入文本。”
不要使用add()
,而是使用setViewportView()
:
反复试验永远不是获得所需布局的好方法。相反,使用JTextArea
构造函数,它可以告诉您需要多少行和列
JTextArea(int rows, int columns)
JTextArea
将在pack()
窗口时计算一个好的首选大小,而不需要setSize()
编辑:您说,“JTextArea
处于非活动状态。我无法在其中输入文本。”
不要使用add()
,而是使用setViewportView()
:
我喜欢SpringLayout,它非常灵活,没有太多不能做的事情。特别是,您将不再需要关心setPreferredSize。只要搜索一下,就有足够的资源了
SpringLayout允许您定义元素相对于其他元素的大小,因此,例如,您可以确保按钮看起来相同。我喜欢SpringLayout,它非常灵活,没有太多不能做到的事情。特别是,您将不再需要关心setPreferredSize。只要搜索一下,就有足够的资源了
SpringLayout允许您定义元素相对于其他元素的大小-例如,您可以确保按钮看起来相同。在这种情况下,我喜欢将我的应用程序划分为责任区。这使代码保持干净和独立,允许我在需要时替换其中的部分,而不会对应用程序的其余部分产生不利影响 这还意味着您可以关注每个部分的各个需求 对于复杂布局,最好(IMHO)使用具有单独布局管理器的复合容器,这样可以降低复杂性和奇怪交叉行为的可能性
public class BadLayout07 {
public static void main(String[] args) {
new BadLayout07();
}
public BadLayout07() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new MasterPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class MasterPane extends JPanel {
public MasterPane() {
EncryptSettings encryptSettings = new EncryptSettings();
InputPane inputPane = new InputPane();
OutputPane outputPane = new OutputPane();
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(encryptSettings, gbc);
gbc.gridy++;
gbc.weighty = 1;
gbc.fill = gbc.BOTH;
add(inputPane, gbc);
gbc.gridy++;
add(outputPane, gbc);
}
}
public class EncryptSettings extends JPanel {
private JRadioButton encrypt;
private JRadioButton decrypt;
private JSpinner factor;
public EncryptSettings() {
encrypt = new JRadioButton("Encrypt");
decrypt = new JRadioButton("Decrypt");
ButtonGroup bg = new ButtonGroup();
bg.add(encrypt);
bg.add(decrypt);
factor = new JSpinner(new SpinnerNumberModel(new Integer(1), new Integer(1), null, new Integer(1)));
setLayout(new FlowLayout(FlowLayout.LEFT));
add(encrypt);
add(decrypt);
add(factor);
}
}
public class InputPane extends JPanel {
private JTextArea input;
private JButton open;
private JButton close;
public InputPane() {
setBorder(new TitledBorder("Source Text"));
input = new JTextArea();
open = new JButton("Open");
close = new JButton("Close");
JPanel tb = new JPanel(new FlowLayout(FlowLayout.LEFT));
tb.add(open);
tb.add(close);
setLayout(new BorderLayout());
add(tb, BorderLayout.NORTH);
add(new JScrollPane(input));
}
}
public class OutputPane extends JPanel {
private JTextArea output;
private JButton save;
private JButton send;
public OutputPane() {
setBorder(new TitledBorder("Encrypted Text"));
output = new JTextArea();
output.setEditable(false);
save = new JButton("Save");
send = new JButton("Send");
JPanel tb = new JPanel(new FlowLayout(FlowLayout.LEFT));
tb.add(save);
tb.add(send);
setLayout(new BorderLayout());
add(tb, BorderLayout.NORTH);
add(new JScrollPane(output));
}
}
}
我没有互连任何功能,但这是一个简单的例子,可以根据需要提供适当的setter和getter,以及适当的事件侦听器。在类似的情况下,我喜欢将我的应用程序划分为责任区域。这使代码保持干净和独立,允许我在需要时替换其中的部分,而不会对应用程序的其余部分产生不利影响 这还意味着您可以关注每个部分的各个需求 对于复杂布局,最好(IMHO)使用具有单独布局管理器的复合容器,这样可以降低复杂性和奇怪交叉行为的可能性
public class BadLayout07 {
public static void main(String[] args) {
new BadLayout07();
}
public BadLayout07() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new MasterPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class MasterPane extends JPanel {
public MasterPane() {
EncryptSettings encryptSettings = new EncryptSettings();
InputPane inputPane = new InputPane();
OutputPane outputPane = new OutputPane();
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(encryptSettings, gbc);
gbc.gridy++;
gbc.weighty = 1;
gbc.fill = gbc.BOTH;
add(inputPane, gbc);
gbc.gridy++;
add(outputPane, gbc);
}
}
public class EncryptSettings extends JPanel {
private JRadioButton encrypt;
private JRadioButton decrypt;
private JSpinner factor;
public EncryptSettings() {
encrypt = new JRadioButton("Encrypt");
decrypt = new JRadioButton("Decrypt");
ButtonGroup bg = new ButtonGroup();
bg.add(encrypt);
bg.add(decrypt);
factor = new JSpinner(new SpinnerNumberModel(new Integer(1), new Integer(1), null, new Integer(1)));
setLayout(new FlowLayout(FlowLayout.LEFT));
add(encrypt);
add(decrypt);
add(factor);
}
}
public class InputPane extends JPanel {
private JTextArea input;
private JButton open;
private JButton close;
public InputPane() {
setBorder(new TitledBorder("Source Text"));
input = new JTextArea();
open = new JButton("Open");
close = new JButton("Close");
JPanel tb = new JPanel(new FlowLayout(FlowLayout.LEFT));
tb.add(open);
tb.add(close);
setLayout(new BorderLayout());
add(tb, BorderLayout.NORTH);
add(new JScrollPane(input));
}
}
public class OutputPane extends JPanel {
private JTextArea output;
private JButton save;
private JButton send;
public OutputPane() {
setBorder(new TitledBorder("Encrypted Text"));
output = new JTextArea();
output.setEditable(false);
save = new JButton("Save");
send = new JButton("Send");
JPanel tb = new JPanel(new FlowLayout(FlowLayout.LEFT));
tb.add(save);
tb.add(send);
setLayout(new BorderLayout());
add(tb, BorderLayout.NORTH);
add(new JScrollPane(output));
}
}
}
我没有互连任何功能,但这只是一个简单的例子,可以根据需要提供适当的setter和getter,以及适当的事件侦听器。
public class Frame扩展了JFrame{< /代码>不扩展框架,当您创建自定义类时,给它一个描述性和可理解的名称,它与J2SE类不一样。ID建议为此。也考虑。<代码>公共类框架扩展JFrm。{ <代码> >不要扩展框架,当你制作一个自定义类时,给它一个与J2SE类不一样的描述性和敏感的名字。ID也推荐这个。也可以考虑。大多数事情都很容易用MigDayStay.;大多数事情都很容易用MigStay.。但是,<代码> JTextArea <代码>是不活动的。我不能在它的文本中输入文本,<代码> JTextArea。
处于非活动状态。我无法在itHa中输入文本!所以你是GridBagLayout的唯一用户,可以让它做你想做的事情!很高兴认识你。:-@keuleJGridBagLayout
非常棒…大多数时候。12年后我想我开始理解它;)@MadProgrammer我只是一个初学者。我一直在尽我最大的努力。这需要时间成为疯子
我还是理智的
:)哈!所以你是GridBagLayout的唯一用户,你真的可以做你想做的事!很高兴认识你。:-@keuleJGridBagLayout
太棒了…大多数时候。12年后我想我开始明白了;)@Madbaglayout我只是一个初学者。我已经尽了最大的努力我会花点时间变得疯狂(我仍然是理智的)