关于swing的问题
packagecom.views;importjava.awt.BorderLayout;importjava.awt.event.ActionEvent;importj...
package com.views;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import com.utils.StringUtil;
public class RecordInfoFRM extends JDialog{
private JButton addBut,cloBut;
private JTextField credientialId,score;
private JRadioButton sex1,sex2;
private JPasswordField pwd;
private JTable jta;
DefaultTableModel df=null;
public RecordInfoFRM(){
JLabel creIdLab = new JLabel("证件号:");
creIdLab.setBounds(10,0,80, 20);
this.add(creIdLab);
credientialId = new JTextField();
credientialId.setBounds(100,0,150,20);
this.add(credientialId);
jta = new JTable();
df = new DefaultTableModel(null,new String[]{"姓名","性别","证件号","入住时间","入住天数","房间号","应付金额","是否付款"});
jta.setModel(df);
JScrollPane js = new JScrollPane(jta);
addBut=new JButton("确认");
addBut.setBounds(280,00,80, 20);
addBut.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
if(!StringUtil.isNotNUll(credientialId.getText())){
JOptionPane.showMessageDialog(null,"请填全内容");
return;
}
if(!StringUtil.isDec(credientialId.getText())){
JOptionPane.showMessageDialog(null,"请填数据");
return;
}
}
});
this.add(addBut);
cloBut = new JButton("关闭");
cloBut.setBounds(700,520,80, 30);
//匿名类
cloBut.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
RecordInfoFRM.this.dispose();
}
});
this.add(cloBut);
this.add(js,BorderLayout.CENTER);
this.setTitle("主界面");
this.setSize(800,600);
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public static void main(String[] args) {
new RecordInfoFRM();
}
}
这里的JTable为什么用setBounds也无法更改位置?我要把它往下挪一点该怎么做 展开
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import com.utils.StringUtil;
public class RecordInfoFRM extends JDialog{
private JButton addBut,cloBut;
private JTextField credientialId,score;
private JRadioButton sex1,sex2;
private JPasswordField pwd;
private JTable jta;
DefaultTableModel df=null;
public RecordInfoFRM(){
JLabel creIdLab = new JLabel("证件号:");
creIdLab.setBounds(10,0,80, 20);
this.add(creIdLab);
credientialId = new JTextField();
credientialId.setBounds(100,0,150,20);
this.add(credientialId);
jta = new JTable();
df = new DefaultTableModel(null,new String[]{"姓名","性别","证件号","入住时间","入住天数","房间号","应付金额","是否付款"});
jta.setModel(df);
JScrollPane js = new JScrollPane(jta);
addBut=new JButton("确认");
addBut.setBounds(280,00,80, 20);
addBut.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
if(!StringUtil.isNotNUll(credientialId.getText())){
JOptionPane.showMessageDialog(null,"请填全内容");
return;
}
if(!StringUtil.isDec(credientialId.getText())){
JOptionPane.showMessageDialog(null,"请填数据");
return;
}
}
});
this.add(addBut);
cloBut = new JButton("关闭");
cloBut.setBounds(700,520,80, 30);
//匿名类
cloBut.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
RecordInfoFRM.this.dispose();
}
});
this.add(cloBut);
this.add(js,BorderLayout.CENTER);
this.setTitle("主界面");
this.setSize(800,600);
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public static void main(String[] args) {
new RecordInfoFRM();
}
}
这里的JTable为什么用setBounds也无法更改位置?我要把它往下挪一点该怎么做 展开
1个回答
展开全部
你只需要新建一个JPanel把creIdLab,credientialId,addBut放进去,把这个JPanel设在窗口北面,
把JTable设在窗口中间,再建一个JPanel把cloBut放进去,把这个JPanel设在窗口南面,就行了。
完整的程序如下:(改动的地方见注释)
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import com.utils.StringUtil;
public class RecordInfoFRM extends JDialog{
private JButton addBut,cloBut;
private JTextField credientialId,score;
private JRadioButton sex1,sex2;
private JPasswordField pwd;
private JTable jta;
private JPanel jp,jp1;//这里加一句
DefaultTableModel df=null;
public RecordInfoFRM(){
jp=new JPanel();//这里加一句
jp.setLayout(new FlowLayout(FlowLayout.LEFT));//这里加一句
JLabel creIdLab = new JLabel("证件号:");
creIdLab.setBounds(10,0,80, 20);//这一句可以去掉
jp.add(creIdLab);//这里把this.add(creIdLab);改成jp.add(creIdLab);
credientialId = new JTextField(20);//这里把new JTextField();改成new JTextField(20);
credientialId.setBounds(100,0,150,20);//这一句可以去掉
jp.add(credientialId);//这里把this.add(credientialId);改成jp.add(credientialId);
jta = new JTable();
df = new DefaultTableModel(null,new String[]{"姓名","性别","证件号","入住时间","入住天数","房间号","应付金额","是否付款"});
jta.setModel(df);
JScrollPane js = new JScrollPane(jta);
addBut=new JButton("确认");
addBut.setBounds(280,00,80, 20);//这一句可以去掉
addBut.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
if(!StringUtil.isNotNUll(credientialId.getText())){
JOptionPane.showMessageDialog(null,"请填全内容");
return;
}
if(!StringUtil.isDec(credientialId.getText())){
JOptionPane.showMessageDialog(null,"请填数据");
return;
}
}
});
jp.add(addBut);//这里把this.add(addBut);改成jp.add(addBut);
this.add(jp,BorderLayout.NORTH);//这里加一句
cloBut = new JButton("关闭");
cloBut.setBounds(700,520,80, 30);//这一句可以去掉
//匿名类
cloBut.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
RecordInfoFRM.this.dispose();
}
});
jp1=new JPanel();//这里加一句
jp1.setLayout(new FlowLayout(FlowLayout.RIGHT));//这里加一句
jp1.add(cloBut);//这里加一句
this.add(jp1,BorderLayout.SOUTH);//这里把this.add(cloBut);改成this.add(jp1,BorderLayout.SOUTH);
this.add(js,BorderLayout.CENTER);
this.setTitle("主界面");
this.setSize(800,600);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//这里JFrame.DO_NOTHING_ON_CLOSE改成JFrame.DISPOSE_ON_CLOSE
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public static void main(String[] args) {
new RecordInfoFRM();
}
}
把JTable设在窗口中间,再建一个JPanel把cloBut放进去,把这个JPanel设在窗口南面,就行了。
完整的程序如下:(改动的地方见注释)
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import com.utils.StringUtil;
public class RecordInfoFRM extends JDialog{
private JButton addBut,cloBut;
private JTextField credientialId,score;
private JRadioButton sex1,sex2;
private JPasswordField pwd;
private JTable jta;
private JPanel jp,jp1;//这里加一句
DefaultTableModel df=null;
public RecordInfoFRM(){
jp=new JPanel();//这里加一句
jp.setLayout(new FlowLayout(FlowLayout.LEFT));//这里加一句
JLabel creIdLab = new JLabel("证件号:");
creIdLab.setBounds(10,0,80, 20);//这一句可以去掉
jp.add(creIdLab);//这里把this.add(creIdLab);改成jp.add(creIdLab);
credientialId = new JTextField(20);//这里把new JTextField();改成new JTextField(20);
credientialId.setBounds(100,0,150,20);//这一句可以去掉
jp.add(credientialId);//这里把this.add(credientialId);改成jp.add(credientialId);
jta = new JTable();
df = new DefaultTableModel(null,new String[]{"姓名","性别","证件号","入住时间","入住天数","房间号","应付金额","是否付款"});
jta.setModel(df);
JScrollPane js = new JScrollPane(jta);
addBut=new JButton("确认");
addBut.setBounds(280,00,80, 20);//这一句可以去掉
addBut.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
if(!StringUtil.isNotNUll(credientialId.getText())){
JOptionPane.showMessageDialog(null,"请填全内容");
return;
}
if(!StringUtil.isDec(credientialId.getText())){
JOptionPane.showMessageDialog(null,"请填数据");
return;
}
}
});
jp.add(addBut);//这里把this.add(addBut);改成jp.add(addBut);
this.add(jp,BorderLayout.NORTH);//这里加一句
cloBut = new JButton("关闭");
cloBut.setBounds(700,520,80, 30);//这一句可以去掉
//匿名类
cloBut.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
RecordInfoFRM.this.dispose();
}
});
jp1=new JPanel();//这里加一句
jp1.setLayout(new FlowLayout(FlowLayout.RIGHT));//这里加一句
jp1.add(cloBut);//这里加一句
this.add(jp1,BorderLayout.SOUTH);//这里把this.add(cloBut);改成this.add(jp1,BorderLayout.SOUTH);
this.add(js,BorderLayout.CENTER);
this.setTitle("主界面");
this.setSize(800,600);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//这里JFrame.DO_NOTHING_ON_CLOSE改成JFrame.DISPOSE_ON_CLOSE
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public static void main(String[] args) {
new RecordInfoFRM();
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询