borderlayout删除按钮(BorderLayout()布局使用) | 您所在的位置:网站首页 › grey什么品牌 › borderlayout删除按钮(BorderLayout()布局使用) |
本文目录BorderLayout()布局使用java BorderLayout布局程序问题,按钮不起作用啊求指导java web 怎么在jtable中添加按钮请问怎么在swing中添加按钮和文本框,然后点击这个按钮后读取文本框里的内容并将自己和文本框删除怎么用JAVA语句在Mysql中查询,添加,删除语句,说的详细点,谢谢!java编程中按钮位置的代码java在borderlayout中怎么改变按钮位置JTable删除选中行JAVA高手来帮帮我java 怎么删除JTable里的某一行BorderLayout()布局使用 JFrame默认布局就是BorderLayout ,所以无需指定布局了 ,我们只需要指定添加的组件的位置就可以了 效果图 参考代码如下 import java.awt.BorderLayout;import java.awt.Color;import javax.swing.*;public class BLDemo extends JFrame {JPanel jp1, jp2, jp3, jp4, jp5;public BLDemo() {jp1 = new JPanel();jp1.add(new JLabel(“东“));add(jp1, BorderLayout.EAST);// 放到窗口的东面jp2 = new JPanel();jp2.add(new JLabel(“南“));add(jp2, BorderLayout.SOUTH);jp3 = new JPanel();jp3.add(new JLabel(“西“));add(jp3, BorderLayout.WEST);jp4 = new JPanel();jp4.add(new JLabel(“北“));add(jp4, BorderLayout.NORTH);jp5 = new JPanel();jp5.setBackground(Color.LIGHT_GRAY);jp5.add(new JLabel(“中“));add(jp5, BorderLayout.CENTER);// BorderLayout.CENTER放到中间,这个可以省略// 窗口属性的设置setTitle(“窗口“);// 窗口标题setSize(300, 300);// 窗口宽 高setLocationRelativeTo(null);// 窗口居中(屏幕中央)setDefaultCloseOperation(EXIT_ON_CLOSE);// 点击窗口右上角的按钮时结束程序}public static void main(String args) {new BLDemo().setVisible(true);//实例化并可见}} java BorderLayout布局程序问题,按钮不起作用啊求指导你的程序是有点问题,我主要修改了两个地方:import java.awt.*;import java.awt.event.*;class WindowButton extends Frame implements ActionListener {Button bSouth;Button bNorth;Button bWest;TextArea bCenter; WindowButton(String s){super(s);setLayout(new BorderLayout());Button bSouth=new Button(“标点“);Button bNorth=new Button(“中文“);Button bWest=new Button(“英文“);bCenter=new TextArea();//修改第1处add(bSouth,BorderLayout.SOUTH);add(bNorth,BorderLayout.NORTH);add(bWest,BorderLayout.WEST);add(bCenter,BorderLayout.CENTER);bSouth.addActionListener(this);bNorth.addActionListener(this);bWest.addActionListener(this);setBounds(100, 100, 300, 300);setVisible(true);validate();}public void actionPerformed(ActionEvent e) {Button btn = (Button)e.getSource();//修改第2处if(btn.getLabel().equals(“中文“)){bCenter.setText(“你按了中文按钮“);}if(btn.getLabel().equals(“标点“)){bCenter.setText(“,.!“);}else if(btn.getLabel().equals(“英文“)){bCenter.setText(“You type the English button“);}}}public class Hello{public static void main(String args) {WindowButton win=new WindowButton(“按钮小练习“);}} java web 怎么在jtable中添加按钮java web在jtable中添加按钮的示例如下: import java.awt.BorderLayout;import java.awt.Color;import java.awt.Component;import java.awt.Dimension;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.io.File;import javax.swing.BorderFactory;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.UIManager;import javax.swing.border.Border;import javax.swing.border.EmptyBorder;import javax.swing.table.AbstractTableModel;import javax.swing.table.TableCellRenderer;public class JTableButton extends JPanel { private JTable table; private JScrollPane scrollPane; private JButton buttons; private String path = System.getProperty(“user.dir“) + File.separator + “images“ + File.separator; public JTableButton() { setBorder(BorderFactory.createLineBorder(Color.red, 1)); init(); } private void init() { String headName = { “Name“, “age“, “sex“, “adress“, “image“ }; buttons = new JButton; for(int i=0;i《buttons.length;i++){ buttons = new JButton(““+i); } Object obj = { { “LiMing“, 23, Boolean.TRUE, buttons, new ImageIcon(path + “icon.png“) }, { “ZhangSan“, 25, Boolean.TRUE,buttons, new ImageIcon(path + “icon.png“) }, { “WangWu“, 21, Boolean.FALSE, buttons, new ImageIcon(path + “icon.png“) }, { “LiSi“, 28, Boolean.TRUE, buttons, new ImageIcon(path + “icon.png“) }, { “LuBo“, 20, Boolean.FALSE, buttons, new ImageIcon(path + “icon.png“) }, }; table = new JTable(new MyTableModel(headName,obj)); table.setDefaultRenderer(JButton.class, new ComboBoxCellRenderer()); scrollPane = new JScrollPane(table); setLayout(new BorderLayout()); add(scrollPane, BorderLayout.CENTER); addHandler(); } private void addHandler(){ //添加事件 table.addMouseListener(new MouseAdapter(){ public void mouseClicked(MouseEvent e) { System.out.println(“table“); int row = table.getSelectedRow(); int column = table.getSelectedColumn(); System.out.println(“row=“+row+“:“+“column=“+column); if(column==3){ //处理button事件写在这里... System.out.println(((JButton)table.getValueAt(row, column)).getText()); } } }); } public static void main(String args) { JFrame frame = new JFrame(); frame.add(new JTableButton()); frame.setSize(new Dimension(800, 400)); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } class MyTableModel extends AbstractTableModel { private String headName; private Object obj; public MyTableModel() { super(); } public MyTableModel(String obj) { this(); this.headName = headName; this.obj = obj; } public int getColumnCount() { return headName.length; } public int getRowCount() { return obj.length; } public Object getValueAt(int r, int c) { return obj; } public String getColumnName(int c) { return headName; } public Class《?》 getColumnClass(int columnIndex) { return obj.getClass(); } @Override public boolean isCellEditable(int rowIndex, int columnIndex) { if (columnIndex == 3 || columnIndex == 4) { return false; } return true; } }}class ComboBoxCellRenderer implements TableCellRenderer { public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { JButton cmb = (JButton) value; if (isSelected) { cmb.setForeground(table.getSelectionForeground()); cmb.setBackground(table.getSelectionBackground()); } else { cmb .setForeground((unselectedForeground != null) ? unselectedForeground : table.getForeground()); cmb .setBackground((unselectedBackground != null) ? unselectedBackground : table.getBackground()); } cmb.setFont(table.getFont()); if (hasFocus) { cmb .setBorder(UIManager .getBorder(“Table.focusCellHighlightBorder“)); if (!isSelected && table.isCellEditable(row, column)) { Color col; col = UIManager.getColor(“Table.focusCellForeground“); if (col != null) { cmb.setForeground(col); } col = UIManager.getColor(“Table.focusCellBackground“); if (col != null) { cmb.setBackground(col); } } } else { cmb.setBorder(noFocusBorder); } return cmb; } protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1); private Color unselectedForeground; private Color unselectedBackground;} 请问怎么在swing中添加按钮和文本框,然后点击这个按钮后读取文本框里的内容并将自己和文本框删除import java.awt.BorderLayout;import java.awt.EventQueue;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.border.EmptyBorder;import javax.swing.JButton;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;import javax.swing.JTextField;import javax.swing.JLabel;public class FrameTest extends JFrame {private JPanel contentPane;private JTextField textField;private JLabel lblNewLabel;/** * Launch the application. */public static void main(String args) {EventQueue.invokeLater(new Runnable() {public void run() {try {FrameTest frame = new FrameTest();frame.setVisible(true);} catch (Exception e) {e.printStackTrace();}}});}/** * Create the frame. */public FrameTest() {setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100, 100, 450, 300);contentPane = new JPanel();contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));setContentPane(contentPane);contentPane.setLayout(null);JButton btnNewButton = new JButton(“确认“);btnNewButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent arg0) {String string = textField.getText();if(!string.equals(““)){ lblNewLabel.setText(string); contentPane.remove(textField); contentPane. remove(btnNewButton); contentPane.updateUI();}}});btnNewButton.setBounds(60, 10, 297, 50);contentPane.add(btnNewButton);textField = new JTextField();textField.setBounds(60, 70, 297, 85);contentPane.add(textField);textField.setColumns(10);lblNewLabel= new JLabel(““);lblNewLabel.setBounds(60, 165, 297, 56);contentPane.add(lblNewLabel);}} 怎么用JAVA语句在Mysql中查询,添加,删除语句,说的详细点,谢谢!创建一个java project;对着project右键-》属性 然后就 如图所示:导入mysql-connector-java-5.1.15-bin.jar!然后就可以添加代码来测试了 下面这个嘛 还有界面的 import java.sql.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class Login extends JFrame { private Connection connection; private Statement statement; private ResultSet resultSet; private ResultSetMetaData rsMetaData; //GUI变量定义 private JTable table; private JTextArea inputQuery; private JButton submitQuery,deleteQuery,insertQuery,alterQuery; public Login() { //Form的标题 super( “输入SQL语句,按查询按钮查看结果。“ ); String url = “jdbc:mysql://localhost:3306/player“; String username = “root“; String password = “mima1231“; //加载驱动程序以连接数据库 try { Class.forName( “org.gjt.mm.mysql.Driver“ ); connection = DriverManager.getConnection( url, username, password ); } //捕获加载驱动程序异常 catch ( ClassNotFoundException cnfex ) { System.err.println( “装载 JDBC/ODBC 驱动程序失败。“ ); cnfex.printStackTrace(); System.exit( 1 ); // terminate program } //捕获连接数据库异常 catch ( SQLException sqlex ) { System.err.println( “无法连接数据库“ ); sqlex.printStackTrace(); System.exit( 1 ); // terminate program } //如果数据库连接成功,则建立GUI //SQL语句 String test=“SELECT * FROM user“; inputQuery = new JTextArea( test, 4, 30 ); submitQuery = new JButton( “查询“ ); //Button事件 submitQuery.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { getTable(); } } ); insertQuery = new JButton(“插入“); deleteQuery = new JButton(“删除“); alterQuery = new JButton(“修改“); JPanel topPanel = new JPanel(); JPanel buttonPanel = new JPanel(); topPanel.setLayout( new BorderLayout() ); //将“输入查询“框布置到 “CENTER“ topPanel.add( new JScrollPane( inputQuery), BorderLayout.CENTER ); //将“提交查询“按钮布置到 “SOUTH“ buttonPanel.setLayout(new GridLayout(1,4)); buttonPanel.add(submitQuery); insertQuery.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String str = inputQuery.getText(); boolean flag = false;
try { // Statement stat = null;
PreparedStatement pstmt = connection.prepareStatement(str);
pstmt.executeUpdate(); flag = true; pstmt.close(); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } if(flag)JOptionPane.showMessageDialog(null, “数据插入成功“); else JOptionPane.showMessageDialog(null, “数据插入失败“);
} }); buttonPanel.add(insertQuery); deleteQuery.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String str = inputQuery.getText(); boolean flag = false; try { // Statement stat = null; Statement pstmt = connection.createStatement();
pstmt.executeUpdate(str); pstmt.close(); flag = true; } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } if(flag)JOptionPane.showMessageDialog(null, “数据删除成功“); else JOptionPane.showMessageDialog(null, “数据删除失败“);
} }); buttonPanel.add(deleteQuery); alterQuery.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String str = inputQuery.getText(); boolean flag = false;
try {
Statement pstmt = connection.createStatement();
pstmt.executeUpdate(str); flag = true; pstmt.close(); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } if(flag)JOptionPane.showMessageDialog(null, “数据修改成功“); else JOptionPane.showMessageDialog(null, “数据修改失败“);
} }); buttonPanel.add(alterQuery); topPanel.add( buttonPanel, BorderLayout.SOUTH ); table = new JTable(); Container c = getContentPane(); c.setLayout( new BorderLayout() ); //将“topPanel“框布置到 “NORTH“ c.add( topPanel, BorderLayout.NORTH ); //将“table“框布置到 “CENTER“ c.add( table, BorderLayout.WEST ); getTable(); setSize( 500, 300 ); //显示Form show(); } private void getTable() { try { //执行SQL语句 String query = inputQuery.getText(); statement = connection.createStatement(); resultSet = statement.executeQuery( query ); //在表格中显示查询结果 displayResultSet( resultSet ); } catch ( SQLException sqlex ) { sqlex.printStackTrace(); } } private void displayResultSet( ResultSet rs ) throws SQLException { //定位到达第一条记录 boolean moreRecords = rs.next(); //如果没有记录,则提示一条消息 if ( ! moreRecords ) { JOptionPane.showMessageDialog( this, “结果集中无记录“ ); setTitle( “无记录显示“ ); return; } Vector columnHeads = new Vector(); Vector rows = new Vector(); try { //获取字段的名称 ResultSetMetaData rsmd = rs.getMetaData(); for ( int i = 1; i 《= rsmd.getColumnCount(); ++i ) columnHeads.addElement( rsmd.getColumnName( i ) ); //获取记录集 do { rows.addElement( getNextRow( rs, rsmd ) ); } while ( rs.next() ); //在表格中显示查询结果 table = new JTable( rows, columnHeads ); JScrollPane scroller = new JScrollPane( table ); Container c = getContentPane(); c.remove(1); c.add( scroller, BorderLayout.CENTER ); //刷新Table c.validate(); } catch ( SQLException sqlex ) { sqlex.printStackTrace(); } } private Vector getNextRow( ResultSet rs, ResultSetMetaData rsmd ) throws SQLException { Vector currentRow = new Vector(); for ( int i = 1; i 《= rsmd.getColumnCount(); ++i ) currentRow.addElement( rs.getString( i ) ); //返回一条记录 return currentRow; } public void shutDown() { try { //断开数据库连接 connection.close(); } catch ( SQLException sqlex ) { System.err.println( “Unable to disconnect“ ); sqlex.printStackTrace(); } } public static void main( String args ) { final Login app = new Login(); app.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { app.shutDown(); System.exit( 0 ); } } ); } } java编程中按钮位置的代码setLayoutManager(new BorderLayout());然后像这样依次添加按钮:(具体添加到面板还是窗体由你自己决定了)add(b1,BorderLayout.south)add(b2,BorderLayout.north)add(b3,BorderLayout.east)add(b4,BorderLayout.west)用了borderlayout之后,setbounds方法是无效的,可以删除这些冗余代码 java在borderlayout中怎么改变按钮位置Java的界面布局,效果最好的就是无布局或者多种布局结合。你的程序中p2这个JPanel使用的默认布局。要改为竖向排列有以下方式:1)GridLayout 网格布局,顶一个一个3行1列的网格就是了。2)BoxLayout 这个就像一个盒子,你只要规定盒子是按照X轴方向排列还是Y轴方向排列。3)GridBagLayout 无序布局,这个比较难用,但用好了效果最好。具体的使用方式你在百度知道里搜索下GridBagLayout 无序布局就能找到,我写过一个比较详细的案例。最后你这种左右分割的布局,建议使用JSplitPane 来控制。这个是一个左右或者上下分割的面板容器。 JTable删除选中行/* 这是一个可运行的例子,看看是不是你要的。*/import java.awt.BorderLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JScrollBar;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.table.DefaultTableModel;public class TableTest extends JFrame{ //声明组件private JTable table;private DefaultTableModel model;private JButton deleteButton;private JPanel panel; public TableTest() {// TODO Auto-generated constructor stub //初始化组件panel = new JPanel();String columnNames = {“编号“,“用户名“,“密码“};Stringdata={{“1“,“zhangsan“,“123456“},{“2“,“lisi“,“4567“}};model = new DefaultTableModel(data, columnNames);table = new JTable(model);deleteButton = new JButton(“删除“);panel = new JPanel();JScrollPane scrollPane = new JScrollPane(table); //添加组件panel.add(scrollPane,BorderLayout.CENTER);panel.add(deleteButton,BorderLayout.SOUTH); this.add(panel);//设置窗口的基本属性.this.setVisible(true);this.setSize(500,500);this.setLocationRelativeTo(null);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //添加事件监听器deleteButton.addActionListener(new ActionListener() { @Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stub//获取要删除的行,没有选择是-1int row = table.getSelectedColumn();if(row == -1){JOptionPane.showMessageDialog(TableTest.this,“请选择要删除的行!“);}else{model.removeRow(row-1);}}});}public static void main(String args) {new TableTest();}} JAVA高手来帮帮我import javax.swing.SwingUtilities; import javax.swing.JPanel; import javax.swing.JFrame; import javax.swing.JTextField; import java.awt.Rectangle; import javax.swing.JLabel; import javax.swing.SwingConstants; import javax.swing.JButton; public class GetSum extends JFrame { private static final long serialVersionUID = 1L; private JPanel jContentPane = null; private JTextField first = null; private JLabel plus = null; private JTextField second = null; private JButton getBt = null; private JTextField sum = null; private JButton clearBt = null; private JTextField getFirst() { if (first == null) { first = new JTextField(); first.setText(“0“); first.setBounds(new Rectangle(5, 19, 80, 22)); } return first; } private JTextField getSecond() { if (second == null) { second = new JTextField(); second.setText(“0“); second.setBounds(new Rectangle(106, 19, 80, 22)); } return second; } private JButton getGetBt() { if (getBt == null) { getBt = new JButton(“=“); getBt.setBounds(new Rectangle(185, 19, 44, 22)); getBt.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { int firstNum = Integer.parseInt(first.getText().trim()); int secondNum = Integer.parseInt(second.getText().trim()); int sumNum = firstNum + secondNum; sum.setText(““+sumNum); } }); } return getBt; } private JTextField getSum() { if (sum == null) { sum = new JTextField(); sum.setEditable(false); sum.setBounds(new Rectangle(229, 19, 102, 22)); } return sum; } private JButton getClearBt() { if (clearBt == null) { clearBt = new JButton(); clearBt.setBounds(new Rectangle(138, 58, 64, 18)); clearBt.setText(“清空“); clearBt.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { first.setText(“0“); second.setText(“0“); sum.setText(““); } }); } return clearBt; } public static void main(String args) { SwingUtilities.invokeLater(new Runnable() { public void run() { GetSum thisClass = new GetSum(); thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); thisClass.setVisible(true); } }); } public GetSum() { super(); initialize(); this.setResizable(false); } private void initialize() { this.setBounds(100, 300, 354, 118); this.setContentPane(getJContentPane()); this.setTitle(“求和“); } private JPanel getJContentPane() { if (jContentPane == null) { plus = new JLabel(); plus.setBounds(new Rectangle(84, 21, 23, 18)); plus.setHorizontalAlignment(SwingConstants.CENTER); plus.setText(“+“); jContentPane = new JPanel(); jContentPane.setLayout(null); jContentPane.add(getFirst(), null); jContentPane.add(plus, null); jContentPane.add(getSecond(), null); jContentPane.add(getGetBt(), null); jContentPane.add(getSum(), null); jContentPane.add(getClearBt(), null); } return jContentPane; } } 是不是要这样的啊?呵呵.不过,要是输入的不是数字的话可不行啊,因为我没有判断输入的是不是数字,你可以自己添加方法。还有,我只是用了整型的数字,你可以改成double类型的。 java 怎么删除JTable里的某一行用int row= jt.getSelectedRow();if(row!=-1)dtm.removeRow(row);就可以删除指定行了。我给你个例子,你看了就明白了。 import java.awt.BorderLayout;import java.awt.Dimension;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.table.DefaultTableModel;public class G {private Object cells={{“some“,0},{“any“,1},{“even“,2}, {“text“,0},{“and“,1},{“text“,2}}; private String columnNames={“Column 1“,“Column 2“}; JFrame jf; JButton jb; JTable jt; JPanel jp,jp1;JScrollPane scrollPane;DefaultTableModel dtm;G(){jf=new JFrame(“JTable“); jb=new JButton(“删除“);jb.addActionListener(new AddActionListener()); dtm = new DefaultTableModel(cells,columnNames);jt = new JTable(dtm);jt.setPreferredScrollableViewportSize(new Dimension(300,120));scrollPane = new JScrollPane(jt); jp=new JPanel();jp1=new JPanel();jp1.add(jb);jp.setLayout(new GridLayout(2,1));jp.add(scrollPane);jp.add(jp1); jf.add(jp,BorderLayout.CENTER);jf.setSize(400,400);jf.setVisible(true);jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}class AddActionListener implements ActionListener{public void actionPerformed(ActionEvent ae) {if(ae.getSource()==jb){int row= jt.getSelectedRow(); //这句选择要删除的行if(row!=-1) //这句判断是否有选中的行dtm.removeRow(row); //这句删除指定行}}}public static void main(String args) {new G();}} |
CopyRight 2018-2019 实验室设备网 版权所有 |