怎么实现在swing的JPanel里面显示信息
我用Java的swing做了一个界面,需要在JPanel面板中显示信息,信息都被保存在一个数组中,现在想把信息一行一行列出来,怎么弄?求教...
我用Java的swing做了一个界面,需要在JPanel面板中显示信息,信息都被保存在一个数组中,现在想把信息一行一行列出来,怎么弄?求教
展开
1个回答
展开全部
用jtable即可
package table;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractCellEditor;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
public class JButtonTable extends JPanel
{
private static final long serialVersionUID = 1L;
public JButtonTable ()
{
super (new BorderLayout ());
JTable table = new JTable (new Model ());
table.setDefaultRenderer (JButton.class, new JButtonRender ());
table.setDefaultEditor (JButton.class, new JButtonEditor ());
table.setPreferredScrollableViewportSize (new Dimension (500, 70));
table.setFillsViewportHeight (true);
table.setSelectionMode (ListSelectionModel.SINGLE_SELECTION);
JScrollPane scrollPane = new JScrollPane (table);
add (scrollPane, BorderLayout.CENTER);
}
class Model extends AbstractTableModel
{
private static final long serialVersionUID = 1L;
JButton modify = new JButton ("修改");
JButton delete = new JButton ("删除");
private String[] columnNames = { "物品编号", "物品名称", "物品数量", "备注", "修改?", "删除?" };
private Object[][] data = { { 1, "水管", 20, "根", modify, delete }, { 2, "窗框", 50, "组", modify, delete },
{ 3, "门", 50, "扇", modify, delete } };
public int getColumnCount ()
{
return columnNames.length;
}
public int getRowCount ()
{
return data.length;
}
public String getColumnName ( int col )
{
return columnNames[col];
}
public Object getValueAt ( int row, int col )
{
return data[row][col];
}
public Class<? extends Object> getColumnClass ( int c )
{
return getValueAt (0, c).getClass ();
}
public boolean isCellEditable ( int row, int col )
{
return true;
}
public void setValueAt ( Object value, int row, int col )
{
data[row][col] = value;
fireTableCellUpdated (row, col);
}
}
private static void createAndShowGUI ()
{
JFrame frame = new JFrame ("JButtonTable");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
JComponent newContentPane = new JButtonTable ();
newContentPane.setOpaque (true);
frame.setContentPane (newContentPane);
frame.pack ();
frame.setLocationRelativeTo (null);
frame.setResizable (false);
frame.setVisible (true);
}
public static void main ( String[] args )
{
javax.swing.SwingUtilities.invokeLater (new Runnable ()
{
public void run ()
{
createAndShowGUI ();
}
});
}
}
class JButtonRender extends JButton implements TableCellRenderer
{
private static final long serialVersionUID = 1L;
protected static final String MODIFY = "修改";
protected static final String DELETE = "删除";
public JButtonRender ()
{
setOpaque (true);
}
@Override
public Component getTableCellRendererComponent ( JTable table, Object jButton, boolean isSelected,
boolean hasFocus, int row, int column )
{
if (column == 4)
{
setText (MODIFY);
}
else if (column == 5)
{
setText (DELETE);
}
setBorder (BorderFactory.createEmptyBorder ());
return this;
}
}
class JButtonEditor extends AbstractCellEditor implements TableCellEditor, ActionListener
{
private static final long serialVersionUID = 1L;
protected static final String MODIFY = "修改";
protected static final String DELETE = "删除";
JButton button;
public JButtonEditor ()
{
button = new JButton ();
button.addActionListener (this);
}
@Override
public Object getCellEditorValue ()
{
return button;
}
@Override
public void actionPerformed ( ActionEvent e )
{
if (MODIFY.equals (e.getActionCommand ()))
{
System.out.println (MODIFY);
fireEditingStopped ();
}
else if (DELETE.equals (e.getActionCommand ()))
{
System.out.println (DELETE);
fireEditingStopped ();
}
}
@Override
public Component getTableCellEditorComponent ( JTable table, Object value, boolean isSelected, int row, int column )
{
if (column == 4)
{
button.setText (MODIFY);
button.setActionCommand (MODIFY);
}
else if (column == 5)
{
button.setText (DELETE);
button.setActionCommand (DELETE);
}
return button;
}
}
package table;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractCellEditor;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
public class JButtonTable extends JPanel
{
private static final long serialVersionUID = 1L;
public JButtonTable ()
{
super (new BorderLayout ());
JTable table = new JTable (new Model ());
table.setDefaultRenderer (JButton.class, new JButtonRender ());
table.setDefaultEditor (JButton.class, new JButtonEditor ());
table.setPreferredScrollableViewportSize (new Dimension (500, 70));
table.setFillsViewportHeight (true);
table.setSelectionMode (ListSelectionModel.SINGLE_SELECTION);
JScrollPane scrollPane = new JScrollPane (table);
add (scrollPane, BorderLayout.CENTER);
}
class Model extends AbstractTableModel
{
private static final long serialVersionUID = 1L;
JButton modify = new JButton ("修改");
JButton delete = new JButton ("删除");
private String[] columnNames = { "物品编号", "物品名称", "物品数量", "备注", "修改?", "删除?" };
private Object[][] data = { { 1, "水管", 20, "根", modify, delete }, { 2, "窗框", 50, "组", modify, delete },
{ 3, "门", 50, "扇", modify, delete } };
public int getColumnCount ()
{
return columnNames.length;
}
public int getRowCount ()
{
return data.length;
}
public String getColumnName ( int col )
{
return columnNames[col];
}
public Object getValueAt ( int row, int col )
{
return data[row][col];
}
public Class<? extends Object> getColumnClass ( int c )
{
return getValueAt (0, c).getClass ();
}
public boolean isCellEditable ( int row, int col )
{
return true;
}
public void setValueAt ( Object value, int row, int col )
{
data[row][col] = value;
fireTableCellUpdated (row, col);
}
}
private static void createAndShowGUI ()
{
JFrame frame = new JFrame ("JButtonTable");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
JComponent newContentPane = new JButtonTable ();
newContentPane.setOpaque (true);
frame.setContentPane (newContentPane);
frame.pack ();
frame.setLocationRelativeTo (null);
frame.setResizable (false);
frame.setVisible (true);
}
public static void main ( String[] args )
{
javax.swing.SwingUtilities.invokeLater (new Runnable ()
{
public void run ()
{
createAndShowGUI ();
}
});
}
}
class JButtonRender extends JButton implements TableCellRenderer
{
private static final long serialVersionUID = 1L;
protected static final String MODIFY = "修改";
protected static final String DELETE = "删除";
public JButtonRender ()
{
setOpaque (true);
}
@Override
public Component getTableCellRendererComponent ( JTable table, Object jButton, boolean isSelected,
boolean hasFocus, int row, int column )
{
if (column == 4)
{
setText (MODIFY);
}
else if (column == 5)
{
setText (DELETE);
}
setBorder (BorderFactory.createEmptyBorder ());
return this;
}
}
class JButtonEditor extends AbstractCellEditor implements TableCellEditor, ActionListener
{
private static final long serialVersionUID = 1L;
protected static final String MODIFY = "修改";
protected static final String DELETE = "删除";
JButton button;
public JButtonEditor ()
{
button = new JButton ();
button.addActionListener (this);
}
@Override
public Object getCellEditorValue ()
{
return button;
}
@Override
public void actionPerformed ( ActionEvent e )
{
if (MODIFY.equals (e.getActionCommand ()))
{
System.out.println (MODIFY);
fireEditingStopped ();
}
else if (DELETE.equals (e.getActionCommand ()))
{
System.out.println (DELETE);
fireEditingStopped ();
}
}
@Override
public Component getTableCellEditorComponent ( JTable table, Object value, boolean isSelected, int row, int column )
{
if (column == 4)
{
button.setText (MODIFY);
button.setActionCommand (MODIFY);
}
else if (column == 5)
{
button.setText (DELETE);
button.setActionCommand (DELETE);
}
return button;
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询