,我想使姓名,手机号和地址都靠左且上下对齐,请问怎么修改?
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
class txl extends JFrame{
//主体框架的元素
private JLabel label1=new JLabel("通讯录");
public JList list=new JList();
private JButton button1=new JButton("添加");
private JButton button2=new JButton("删除");
private JButton button3=new JButton("修改");
private JPanel panel=new JPanel();
//字符串数组
public String[] str=new String[100];
public String[] str1=new String[100];
public String[] str2=new String[100];
public String[] str4=new String[100];
public int i=4;
public int n=0;
public int[] xz;
//添加的框架的元素
public JTextField tf1=new JTextField(20);
public JTextField tf2=new JTextField(20);
public JTextField tf4=new JTextField(20);
private JButton button5=new JButton("确定添加");
//添加框架类作为主体类的一个数据成员
class 添加按钮 extends JFrame{
private JLabel label1=new JLabel("姓名");
private JLabel label2=new JLabel("手机号");
private JLabel label4=new JLabel("地址");
private JPanel panelNorth=new JPanel();
public 添加按钮(){
Container contentPane=getContentPane();
panelNorth.setLayout(new GridLayout(4,2,5,5));
panelNorth.add(label1);
panelNorth.add(tf1);
panelNorth.add(label2);
panelNorth.add(tf2);
panelNorth.add(label4);
panelNorth.add(tf4);
contentPane.add(panelNorth,BorderLayout.NORTH);
contentPane.add(button5,BorderLayout.CENTER);
setBounds(300,300,300,300);
button5.setBackground(Color.GREEN);
pack();
setVisible(true);
}
}
// 删除的框架元素
public JTextField tf5=new JTextField(60);
private JButton button6=new JButton("确认删除");
//删除框架类作为主体的数据成员
class 删除按钮 extends JFrame{
private JPanel panelCenter=new JPanel();
public 删除按钮(){
Container contentPane=getContentPane();
panelCenter.add(button6);
contentPane.add(tf5,BorderLayout.NORTH);
contentPane.add(button6,BorderLayout.CENTER);
setBounds(300,300,300,500);
button6.setBackground(Color.GREEN);
pack();
setVisible(true);
}
}
//修改的框架元素
public JTextField tf6=new JTextField(20);
public JTextField tf7=new JTextField(20);
public JTextField tf8=new JTextField(20);
public JTextField tf9=new JTextField(20);
public JButton button7=new JButton("确认修改");
//修改类作为主体的数据成员
class 修改按钮 extends JFrame{
private JLabel label1=new JLabel("姓名");
private JLabel label2=new JLabel("手机号");
private JLabel label4=new JLabel("地址");
private JPanel panelNorth=new JPanel();
public 修改按钮(){
Container contentPane=getContentPane();
panelNorth.setLayout(new GridLayout(4,2,5,5));
panelNorth.add(label1);
panelNorth.add(tf6);
panelNorth.add(label2);
panelNorth.add(tf7);
panelNorth.add(tf8);
panelNorth.add(label4);
panelNorth.add(tf9);
contentPane.add(panelNorth,BorderLayout.NORTH);
contentPane.add(button7,BorderLayout.CENTER);
setBounds(300,300,300,300);
button7.setBackground(Color.GREEN);
pack();
setVisible(true);
}
}
//查找的框架元素
public JTextField tf10=new JTextField();
public JTextField tf11=new JTextField(40);
public JButton button8=new JButton("确定查找");
//查找类作为主体类的数据成员
class 查找按钮 extends JFrame{
private JLabel label1=new JLabel("请输入要查找的姓名");
private JPanel panelNorth=new JPanel();
public 查找按钮(){
tf10.setText("");
tf11.setText("");
Container contentPane=getContentPane();
panelNorth.setLayout(new GridLayout(1,2,5,5));
panelNorth.add(label1);
panelNorth.add(tf10);
contentPane.add(panelNorth,BorderLayout.NORTH);
contentPane.add(tf11,BorderLayout.CENTER);
contentPane.add(button8,BorderLayout.SOUTH);
setBounds(300,300,300,300);
button8.setBackground(Color.GREEN);
pack();
setVisible(true);
}
}
//成员方法
public txl(String title){
super(title);
//初始化数组
str[0]="徐天娇"+" 手机号 "+"137jjjj6355"+" 地址 "+"郑州";
str[1]="万剑一"+" 手机号 "+"152jjjj720"+" 地址 "+"北京";
str[2]="范华强"+" 手机号 "+"153iiii0302"+" 地址 "+"泉州";
str[3]="张建波"+" 手机号 "+"150jjjj689"+" 地址 "+"保定";
str1[0]="徐天娇";str2[0]="13781596355";str4[0]="郑州";
str1[1]="万剑一";str2[1]="15230257720";str4[1]="北京";
str1[2]="范华强";str2[2]="15038077302";str4[2]="泉州";
str1[3]="张建波";str2[3]="15097703689";str4[3]="保定";
list.setListData(str);
//设置主体框架
list.setVisibleRowCount(10);
Container contentPane=this.getContentPane();
contentPane.add(label1,BorderLayout.NORTH);
contentPane.add(new JScrollPane(list),BorderLayout.CENTER);
panel.setLayout(new FlowLayout());
panel.add(button1);
panel.add(button2);
panel.add(button3);
contentPane.add(panel,BorderLayout.SOUTH);
setBounds(300,300,300,300);
//设置窗体颜色
list.setBackground(Color.pink);
//label1.setBackground(Color.purple);
button1.setBackground(Color.orange);
button2.setBackground(Color.orange);
button3.setBackground(Color.orange);
pack();
setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//之后注册监听器
list.addListSelectionListener(new MyActionListener());
button1.addActionListener(new MyActionListener());
button2.addActionListener(new MyActionListener());
button3.addActionListener(new MyActionListener());
button5.addActionListener(new MyActionListener());
button6.addActionListener(new MyActionListener());
button7.addActionListener(new MyActionListener());
button8.addActionListener(new MyActionListener());
}
//实现监听器
private class MyActionListener implements ActionListener,ListSelectionListener{
// 实现LIST选择监听
public void valueChanged(ListSelectionEvent arg0){
n=list.getSelectedIndex();
xz=list.getSelectedIndices();
}
//实现按钮功能
public void actionPerformed(ActionEvent e){
String command=e.getActionCommand();
//实现添加按钮功能
if(command.equals("添加")){
new 添加按钮();
}
if(command.equals("确定添加")){
if(tf1.getText().length()==0){
tf1.setText("请输入姓名");
return;
}
if(tf2.getText().length()==0){
tf2.setText("请输入手机号码");
return;
}
if(tf4.getText().length()==0){
tf4.setText("请输入地址");
return;
}
else{
str1[i]=tf1.getText();
str2[i]=tf2.getText();
//str3[i]=tf3.getText();
str4[i]=tf4.getText();
str[i]=new String(tf1.getText()+" 手机号 "+tf2.getText()+" 地址 "+tf4.getText());
list.setListData(str);
i++;
tf1.setText("");
tf2.setText("");
tf4.setText("");
}
}
//实现删除按钮功能
if(command.equals("删除")){
new 删除按钮();
if(xz.length==1){
tf5.setText("确认要删除 "+str[n]);
}
else{
for(int i=0;i<xz.length;i++){
tf5.setText("确认要删除这几项吗?");
}
}
}
if(command.equals("确认删除")){
for(int a=0;a<xz.length;a++){
for(int k=n;k<i;k++){
str[k]=str[k+1];
}
i--;
}
list.setListData(str);
tf5.setText("");
}
//实现修改按钮功能
if(command.equals("修改")){
new 修改按钮();
tf6.setText(str1[n]);
tf7.setText(str2[n]);
tf9.setText(str4[n]);
}
if(command.equals("确认修改")){
str1[n]=tf6.getText();
str2[n]=tf7.getText();
str4[n]=tf9.getText();
str[n]=new String(tf6.getText()+" 手机号 "+tf7.getText()+" 地址 "+tf9.getText());
list.setListData(str);
tf6.setText("");
tf7.setText("");
tf8.setText("");
tf9.setText("");
}
//实现查找按钮功能
if(command.equals("查找")){
new 查找按钮();
}
if(command.equals("确定查找")){
if(tf10.getText().length()==0){
tf11.setText("请输入要查找的姓名");
return;
}
else{
int x=0;
int m=0;
for(;m<i;m++){
if(str1[m].compareTo(tf10.getText())==0){
break;
}
x=m;
}
if(x==i-1){
tf11.setText("对不起没有找到,请您重新输入");
}
else{
if(x==0&&m==0){
tf11.setText("您要找的是 "+str1[x]+" 手机号"+str2[x]+" 地址 "+str4[x]);
}
else{
tf11.setText("您要找的是 "+str1[x+1]+" 手机号"+str2[x+1]+" 地址 "+str4[x+1]);
}
}
}
}
}
}
}
public class 通讯录代码 { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new txl("通讯录"); }}
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
class txl extends JFrame{
//主体框架的元素
private JLabel label1=new JLabel("通讯录");
public JList list=new JList();
private JButton button1=new JButton("添加");
private JButton button2=new JButton("删除");
private JButton button3=new JButton("修改");
private JPanel panel=new JPanel();
//字符串数组
public String[] str=new String[100];
public String[] str1=new String[100];
public String[] str2=new String[100];
public String[] str4=new String[100];
public int i=4;
public int n=0;
public int[] xz;
//添加的框架的元素
public JTextField tf1=new JTextField(20);
public JTextField tf2=new JTextField(20);
public JTextField tf4=new JTextField(20);
private JButton button5=new JButton("确定添加");
//添加框架类作为主体类的一个数据成员
class 添加按钮 extends JFrame{
private JLabel label1=new JLabel("姓名");
private JLabel label2=new JLabel("手机号");
private JLabel label4=new JLabel("地址");
private JPanel panelNorth=new JPanel();
public 添加按钮(){
Container contentPane=getContentPane();
panelNorth.setLayout(new GridLayout(4,2,5,5));
panelNorth.add(label1);
panelNorth.add(tf1);
panelNorth.add(label2);
panelNorth.add(tf2);
panelNorth.add(label4);
panelNorth.add(tf4);
contentPane.add(panelNorth,BorderLayout.NORTH);
contentPane.add(button5,BorderLayout.CENTER);
setBounds(300,300,300,300);
button5.setBackground(Color.GREEN);
pack();
setVisible(true);
}
}
// 删除的框架元素
public JTextField tf5=new JTextField(60);
private JButton button6=new JButton("确认删除");
//删除框架类作为主体的数据成员
class 删除按钮 extends JFrame{
private JPanel panelCenter=new JPanel();
public 删除按钮(){
Container contentPane=getContentPane();
panelCenter.add(button6);
contentPane.add(tf5,BorderLayout.NORTH);
contentPane.add(button6,BorderLayout.CENTER);
setBounds(300,300,300,500);
button6.setBackground(Color.GREEN);
pack();
setVisible(true);
}
}
//修改的框架元素
public JTextField tf6=new JTextField(20);
public JTextField tf7=new JTextField(20);
public JTextField tf8=new JTextField(20);
public JTextField tf9=new JTextField(20);
public JButton button7=new JButton("确认修改");
//修改类作为主体的数据成员
class 修改按钮 extends JFrame{
private JLabel label1=new JLabel("姓名");
private JLabel label2=new JLabel("手机号");
private JLabel label4=new JLabel("地址");
private JPanel panelNorth=new JPanel();
public 修改按钮(){
Container contentPane=getContentPane();
panelNorth.setLayout(new GridLayout(4,2,5,5));
panelNorth.add(label1);
panelNorth.add(tf6);
panelNorth.add(label2);
panelNorth.add(tf7);
panelNorth.add(tf8);
panelNorth.add(label4);
panelNorth.add(tf9);
contentPane.add(panelNorth,BorderLayout.NORTH);
contentPane.add(button7,BorderLayout.CENTER);
setBounds(300,300,300,300);
button7.setBackground(Color.GREEN);
pack();
setVisible(true);
}
}
//查找的框架元素
public JTextField tf10=new JTextField();
public JTextField tf11=new JTextField(40);
public JButton button8=new JButton("确定查找");
//查找类作为主体类的数据成员
class 查找按钮 extends JFrame{
private JLabel label1=new JLabel("请输入要查找的姓名");
private JPanel panelNorth=new JPanel();
public 查找按钮(){
tf10.setText("");
tf11.setText("");
Container contentPane=getContentPane();
panelNorth.setLayout(new GridLayout(1,2,5,5));
panelNorth.add(label1);
panelNorth.add(tf10);
contentPane.add(panelNorth,BorderLayout.NORTH);
contentPane.add(tf11,BorderLayout.CENTER);
contentPane.add(button8,BorderLayout.SOUTH);
setBounds(300,300,300,300);
button8.setBackground(Color.GREEN);
pack();
setVisible(true);
}
}
//成员方法
public txl(String title){
super(title);
//初始化数组
str[0]="徐天娇"+" 手机号 "+"137jjjj6355"+" 地址 "+"郑州";
str[1]="万剑一"+" 手机号 "+"152jjjj720"+" 地址 "+"北京";
str[2]="范华强"+" 手机号 "+"153iiii0302"+" 地址 "+"泉州";
str[3]="张建波"+" 手机号 "+"150jjjj689"+" 地址 "+"保定";
str1[0]="徐天娇";str2[0]="13781596355";str4[0]="郑州";
str1[1]="万剑一";str2[1]="15230257720";str4[1]="北京";
str1[2]="范华强";str2[2]="15038077302";str4[2]="泉州";
str1[3]="张建波";str2[3]="15097703689";str4[3]="保定";
list.setListData(str);
//设置主体框架
list.setVisibleRowCount(10);
Container contentPane=this.getContentPane();
contentPane.add(label1,BorderLayout.NORTH);
contentPane.add(new JScrollPane(list),BorderLayout.CENTER);
panel.setLayout(new FlowLayout());
panel.add(button1);
panel.add(button2);
panel.add(button3);
contentPane.add(panel,BorderLayout.SOUTH);
setBounds(300,300,300,300);
//设置窗体颜色
list.setBackground(Color.pink);
//label1.setBackground(Color.purple);
button1.setBackground(Color.orange);
button2.setBackground(Color.orange);
button3.setBackground(Color.orange);
pack();
setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//之后注册监听器
list.addListSelectionListener(new MyActionListener());
button1.addActionListener(new MyActionListener());
button2.addActionListener(new MyActionListener());
button3.addActionListener(new MyActionListener());
button5.addActionListener(new MyActionListener());
button6.addActionListener(new MyActionListener());
button7.addActionListener(new MyActionListener());
button8.addActionListener(new MyActionListener());
}
//实现监听器
private class MyActionListener implements ActionListener,ListSelectionListener{
// 实现LIST选择监听
public void valueChanged(ListSelectionEvent arg0){
n=list.getSelectedIndex();
xz=list.getSelectedIndices();
}
//实现按钮功能
public void actionPerformed(ActionEvent e){
String command=e.getActionCommand();
//实现添加按钮功能
if(command.equals("添加")){
new 添加按钮();
}
if(command.equals("确定添加")){
if(tf1.getText().length()==0){
tf1.setText("请输入姓名");
return;
}
if(tf2.getText().length()==0){
tf2.setText("请输入手机号码");
return;
}
if(tf4.getText().length()==0){
tf4.setText("请输入地址");
return;
}
else{
str1[i]=tf1.getText();
str2[i]=tf2.getText();
//str3[i]=tf3.getText();
str4[i]=tf4.getText();
str[i]=new String(tf1.getText()+" 手机号 "+tf2.getText()+" 地址 "+tf4.getText());
list.setListData(str);
i++;
tf1.setText("");
tf2.setText("");
tf4.setText("");
}
}
//实现删除按钮功能
if(command.equals("删除")){
new 删除按钮();
if(xz.length==1){
tf5.setText("确认要删除 "+str[n]);
}
else{
for(int i=0;i<xz.length;i++){
tf5.setText("确认要删除这几项吗?");
}
}
}
if(command.equals("确认删除")){
for(int a=0;a<xz.length;a++){
for(int k=n;k<i;k++){
str[k]=str[k+1];
}
i--;
}
list.setListData(str);
tf5.setText("");
}
//实现修改按钮功能
if(command.equals("修改")){
new 修改按钮();
tf6.setText(str1[n]);
tf7.setText(str2[n]);
tf9.setText(str4[n]);
}
if(command.equals("确认修改")){
str1[n]=tf6.getText();
str2[n]=tf7.getText();
str4[n]=tf9.getText();
str[n]=new String(tf6.getText()+" 手机号 "+tf7.getText()+" 地址 "+tf9.getText());
list.setListData(str);
tf6.setText("");
tf7.setText("");
tf8.setText("");
tf9.setText("");
}
//实现查找按钮功能
if(command.equals("查找")){
new 查找按钮();
}
if(command.equals("确定查找")){
if(tf10.getText().length()==0){
tf11.setText("请输入要查找的姓名");
return;
}
else{
int x=0;
int m=0;
for(;m<i;m++){
if(str1[m].compareTo(tf10.getText())==0){
break;
}
x=m;
}
if(x==i-1){
tf11.setText("对不起没有找到,请您重新输入");
}
else{
if(x==0&&m==0){
tf11.setText("您要找的是 "+str1[x]+" 手机号"+str2[x]+" 地址 "+str4[x]);
}
else{
tf11.setText("您要找的是 "+str1[x+1]+" 手机号"+str2[x+1]+" 地址 "+str4[x+1]);
}
}
}
}
}
}
}
public class 通讯录代码 { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new txl("通讯录"); }}
代码结构很乱,类名居然还用中文?
class 修改按钮 extends JFrame {
private JLabel label1 = new JLabel("姓名");
private JLabel label2 = new JLabel("手机号");
private JLabel label4 = new JLabel("地址");
private JPanel panelNorth = new JPanel(); public 修改按钮() {
Container contentPane = getContentPane();
panelNorth.setLayout(new GridLayout(4, 2, 5, 5));
panelNorth.add(label1);
panelNorth.add(tf6);
panelNorth.add(label2);
panelNorth.add(tf7);
//****************************************************
//panelNorth.add(tf8);
//****************************************************
panelNorth.add(label4);
panelNorth.add(tf9);
contentPane.add(panelNorth, BorderLayout.NORTH);
contentPane.add(button7, BorderLayout.CENTER);
setBounds(300, 300, 300, 300);
button7.setBackground(Color.GREEN);
pack();
setVisible(true);
}
}注释处的内容去掉就好了!
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;public class ModifyFrame extends JFrame { private JLabel label1 = new JLabel("姓名");
private JLabel label2 = new JLabel("手机号");
private JLabel label4 = new JLabel("地址");
private JTextField tf6 = new JTextField();
private JTextField tf7 = new JTextField();
private JTextField tf9 = new JTextField();
private JButton button7 = new JButton("确认修改");
private JPanel panelNorth = new JPanel(); public ModifyFrame() {
Container contentPane = getContentPane();
panelNorth.setLayout(new GridLayout(3, 2, 5, 5));
panelNorth.add(label1);
panelNorth.add(tf6);
panelNorth.add(label2);
panelNorth.add(tf7);
//****************************************************
//panelNorth.add(tf8);
//****************************************************
panelNorth.add(label4);
panelNorth.add(tf9);
contentPane.add(panelNorth, BorderLayout.NORTH);
contentPane.add(button7, BorderLayout.SOUTH);
// setBounds(300, 300, 300, 300);
button7.setBackground(Color.GREEN);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// pack();
this.setSize(new Dimension(300, 150));
setVisible(true);
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new ModifyFrame();
}}
效果正常.
GridLayout布局不好,用GridBagLayout手动控制得精准些.类名不要用中文,看着就别扭.