我是Java的新手,我写过这个简单的案例转换器程序:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class caseconversion extends JFrame{
private JTextField tf;
private JCheckBox boldbox;
private JCheckBox italicbox;
private JButton up;
private JButton low;
public caseconversion(){
super ("Case Converter");
setLayout(new FlowLayout());
tf=new JTextField("Hello whats up Buddy !!",25);
tf.setFont(new Font("Segoe Print",Font.PLAIN,15));
add(tf);
boldbox = new JCheckBox("Bold");
italicbox = new JCheckBox("Italic");
add(boldbox);
add(italicbox);
up=new JButton("Upper Case");
low=new JButton("Lowercase");
add(up);
add(low);
HandlerClass handler = new HandlerClass();
boldbox.addItemListener(handler);
italicbox.addItemListener(handler);
up.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
tf.setText(tf.getText().toUpperCase());
}
}
);
low.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
tf.setText(tf.getText().toLowerCase());
}
}
);
}
private class HandlerClass implements ItemListener {
//For Checkboxes
public void itemStateChanged(ItemEvent event){
Font font= null;
if(boldbox.isSelected()&&italicbox.isSelected())
font = new Font("Segoe Print",Font.BOLD + Font.ITALIC ,15);
else if(boldbox.isSelected())
font = new Font("Segoe Print",Font.BOLD ,15);
else if(italicbox.isSelected())
font = new Font("Segoe Print",Font.ITALIC ,15);
else
font= new Font("Segoe Print",Font.PLAIN,15);
tf.setFont(font);
}
}
public static void main(String arg[]){
caseconversion go = new caseconversion();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(400,250);
go.setVisible(true);
}
}
它工作正常,但我希望在中心的下一行显示JTextField tf
,boldbox
和italicbok
,同样在中心的第3行显示JButtons
你能吗?请告诉我该怎么做。
答案 0 :(得分:0)
如果你使用eclipse,我知道一种帮助我的方法。 JWindowbuilder:http://www.eclipse.org/windowbuilder/
如果不是,你也应该看一下: http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
问候Visores
我用WindowBuilder改变了你想要的方式,我希望你喜欢它:D
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class caseconversion extends JFrame {
private JTextField tf;
private JCheckBox boldbox;
private JCheckBox italicbox;
private JButton up;
private JButton low;
private Box verticalBox;
private Box horizontalBox;
private Box horizontalBox_1;
private Box horizontalBox_2;
private Component horizontalStrut;
private Component horizontalStrut_1;
private Component verticalStrut;
public caseconversion() {
super("Case Converter");
HandlerClass handler = new HandlerClass();
getContentPane().setLayout(
new BoxLayout(getContentPane(), BoxLayout.X_AXIS));
verticalBox = Box.createVerticalBox();
getContentPane().add(verticalBox);
horizontalBox = Box.createHorizontalBox();
verticalBox.add(horizontalBox);
horizontalStrut_1 = Box.createHorizontalStrut(20);
horizontalBox.add(horizontalStrut_1);
tf = new JTextField("Hello whats up Buddy !!", 25);
tf.setMaximumSize(new Dimension(2147483647, 60));
horizontalBox.add(tf);
tf.setFont(new Font("Segoe Print", Font.PLAIN, 15));
horizontalStrut = Box.createHorizontalStrut(20);
horizontalBox.add(horizontalStrut);
horizontalBox_1 = Box.createHorizontalBox();
verticalBox.add(horizontalBox_1);
boldbox = new JCheckBox("Bold");
horizontalBox_1.add(boldbox);
italicbox = new JCheckBox("Italic");
horizontalBox_1.add(italicbox);
italicbox.addItemListener(handler);
boldbox.addItemListener(handler);
horizontalBox_2 = Box.createHorizontalBox();
verticalBox.add(horizontalBox_2);
up = new JButton("Upper Case");
horizontalBox_2.add(up);
low = new JButton("Lowercase");
horizontalBox_2.add(low);
verticalStrut = Box.createVerticalStrut(20);
verticalBox.add(verticalStrut);
low.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
tf.setText(tf.getText().toLowerCase());
}
});
up.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
tf.setText(tf.getText().toUpperCase());
}
});
}
private class HandlerClass implements ItemListener {
// For Checkboxes
public void itemStateChanged(ItemEvent event) {
Font font = null;
if (boldbox.isSelected() && italicbox.isSelected())
font = new Font("Segoe Print", Font.BOLD + Font.ITALIC, 15);
else if (boldbox.isSelected())
font = new Font("Segoe Print", Font.BOLD, 15);
else if (italicbox.isSelected())
font = new Font("Segoe Print", Font.ITALIC, 15);
else
font = new Font("Segoe Print", Font.PLAIN, 15);
tf.setFont(font);
}
}
public static void main(String arg[]) {
caseconversion go = new caseconversion();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(400, 250);
go.setVisible(true);
}
}
答案 1 :(得分:0)
好吧,你可以为JTextField添加BorderLayout,如下所示:
add(tf,BorderLayout.NORTH);
并添加2个JPanel项目: -
private JPanel panel1;
private JPanel panel2;
并将它们初始化为构造函数,如下所示: -
panel1 =new JPanel();
panel2 =new JPanel();
并添加此代码
panel1.add(boldbox);
panel1.add(italicbox);
add(panel1,BorderLayout.CENTER);
panel2.add(up);
panel2.add(low);
add(panel2,BorderLayout.SOUTH);
我希望这对你有所帮助。
答案 2 :(得分:0)
最简单的解决方案(对原始代码进行最小的更改)可能是将每行放入一个JPanel(每个JPanel都有一个FlowLayout),并使用GridLayout安排这3个面板
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class caseconversion extends JFrame{
private JTextField tf;
private JCheckBox boldbox;
private JCheckBox italicbox;
private JButton up;
private JButton low;
public caseconversion(){
super ("Case Converter");
setLayout(new GridLayout(0,1));
JPanel p = null;
p = new JPanel();
tf=new JTextField("Hello whats up Buddy !!",25);
tf.setFont(new Font("Segoe Print",Font.PLAIN,15));
p.add(tf);
add(p);
p = new JPanel();
boldbox = new JCheckBox("Bold");
italicbox = new JCheckBox("Italic");
p.add(boldbox);
p.add(italicbox);
add(p);
p = new JPanel();
up=new JButton("Upper Case");
low=new JButton("Lowercase");
p.add(up);
p.add(low);
add(p);
HandlerClass handler = new HandlerClass();
boldbox.addItemListener(handler);
italicbox.addItemListener(handler);
up.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
tf.setText(tf.getText().toUpperCase());
}
}
);
low.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
tf.setText(tf.getText().toLowerCase());
}
}
);
}
private class HandlerClass implements ItemListener {
//For Checkboxes
public void itemStateChanged(ItemEvent event){
Font font= null;
if(boldbox.isSelected()&&italicbox.isSelected())
font = new Font("Segoe Print",Font.BOLD + Font.ITALIC ,15);
else if(boldbox.isSelected())
font = new Font("Segoe Print",Font.BOLD ,15);
else if(italicbox.isSelected())
font = new Font("Segoe Print",Font.ITALIC ,15);
else
font= new Font("Segoe Print",Font.PLAIN,15);
tf.setFont(font);
}
}
public static void main(String arg[]){
caseconversion go = new caseconversion();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(400,250);
go.setVisible(true);
}
}
请注意,您还必须考虑调整大小的行为。这三排是否总是跨越框架的整个高度,还是应该紧紧地推到框架的顶部边缘?
然而,一般提示(似乎被新手低估)是嵌套:创建新的JPanel以实现特定布局没有任何问题。通常,实现这一目标的更“干净”的解决方案是逻辑地对几个GUI组件进行分组(而不是重复使用像我在示例中使用的JPanel p
这样的辅助变量。)