gpt4 book ai didi

java - 如何在我的图形用户界面中绘制准确的行数?

转载 作者:行者123 更新时间:2023-12-02 05:36:24 25 4
gpt4 key购买 nike

我正在尝试在我的图形用户界面中绘制确切的行数。因此,我使用 TableModelListener 来监听 Jtable 中的行数,每次在表中添加新行时,我都会设法显示行数。

问题:但是现在,当我的 jtable 中有 x 行时,我想在 gui 中绘制 x 行。我尝试像上面那样放置一个 TableModelListerner 但它不起作用。

感谢您的帮助。我很抱歉放置了很多类(class),但我还想学习编写可重用的代码并学习 java swing。

enter image description here

类测试

import java.awt.GridLayout;

import javax.swing.JFrame;

public class Test {

public static void main(String[] args) {
JFrame f = new JFrame();

f.setLayout(new GridLayout(1, 2, 5, 5));

TablePanel tablePanel = new TablePanel();
ButtonPanel buttonPanel = new ButtonPanel();
DrawTwoSameDrawPanel drawPanels = new DrawTwoSameDrawPanel();

f.add(tablePanel);
f.add(buttonPanel);
f.add(drawPanels);

f.setSize(800, 300);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);

}

}

MyTableModel 类

import javax.swing.table.AbstractTableModel;

public class MyTableModel extends AbstractTableModel {

private String[] columnNames = { "RowListener", "B", "C" };
private Object[][] data = { { "x", "xx", "xxx" }, { "y", "yy", "yyy" }, { "z", "zz", "zzz" } };

@Override
public int getColumnCount() {
return columnNames.length;
}

@Override
public int getRowCount() {
return data.length;
}

@Override
public Object getValueAt(int row, int col) {
return data[row][col];
}

@Override
public Class<?> getColumnClass(int col) {
return data[0][col].getClass();
}

@Override
public String getColumnName(int col) {
return columnNames[col];
}

@Override
public void setValueAt(Object aValue, int row, int col) {
data[row][col] = aValue;
fireTableStructureChanged();
}

public void addRow(Object[][] data) {
int index = 0;
int nbRow = this.getRowCount();
int nbCol = this.getColumnCount();

Object temp[][] = this.data;
this.data = new Object[nbRow + 1][nbCol];

for (Object[] value : temp)
this.data[index++] = value;

this.data[index] = data;
temp = null;

this.fireTableDataChanged();

}

}

类 ButtonPanel

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

public class ButtonPanel extends JPanel {

private JButton button;
private DrawPanel drawPanel;

public ButtonPanel() {
super();

this.setBorder(new LineBorder(Color.red));
button = new JButton("Update");
this.add(button);
button.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("hello console");

int n = JOptionPane.showConfirmDialog(null, "Would you like to update drawPanel ?", "Question",
JOptionPane.YES_NO_OPTION);

if (n == JOptionPane.YES_OPTION) {
System.out.println("console : YES");
updatePanels();
}
else
System.out.println("console : NO");
}

});
}

public void updatePanels() {
drawPanel = new DrawPanel();
drawPanel.revalidate();
drawPanel.repaint();

}
}

DrawPanel类

public class DrawPanel extends JPanel implements TableModelListener{

private MyTableModel model = new MyTableModel();
private JTable table = new JTable(model);

private int numberOfLine = 0;

public DrawPanel() {
super();
this.setBorder(new LineBorder(Color.blue));
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics graphics = g.create();
int y = 10;

/////////don't work
RowListener rowListener = new RowListener(model);
numberOfLine = rowListener.getNumberOfRow();
//////////

for (int i = 0; i < numberOfLine; i++) {
graphics.drawLine(10, y, getWidth() - 10, y);
y += 10;
}
graphics.dispose();
}

public void setNumberOfArrows(int arrows) {
numberOfLine = arrows;
repaint();
}

public int getNumberOfArrows() {
return numberOfLine;
}

@Override
public Dimension getPreferredSize() {
return isPreferredSizeSet() ? super.getPreferredSize() : new Dimension(200, 200);
}

@Override
public void tableChanged(TableModelEvent arg0) {
int numberOfRow = model.getRowCount();
this.setNumberOfArrows(numberOfRow);
System.out.println("numberOfRow: " + numberOfRow);

}

class RowListener implements TableModelListener {
private MyTableModel model;
private int numberOfRow;

public RowListener(MyTableModel model) {
this.model = model;
model.addTableModelListener(this);
}

@Override
public void tableChanged(TableModelEvent arg0) {
numberOfRow = model.getRowCount();
System.out.println("numberOfRow: " + numberOfRow);
}

public int getNumberOfRow() {
return numberOfRow;
}

public void setNumberOfRow(int numberOfRow) {
this.numberOfRow = numberOfRow;
}
}

}

类TablePanel

public class TablePanel extends JPanel {

private MyTableModel model = new MyTableModel();
private JTable table = new JTable(model);
private int nbRow = 0;

private JButton addButton = new JButton("Add Row");

public TablePanel() {
super();
this.setBorder(new LineBorder(Color.orange));
this.add(table);
this.add(addButton);

addButton.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent arg0) {
model.addRow(new Object[][] { { "u", "u", "u" }, { "v", "v", "vy" }, { "w", "w", "w" } });
model.fireTableStructureChanged();
nbRow = model.getRowCount();
System.out.println("nbRow: " + nbRow);

}
});

this.add(createDisplayNbRowPanel());

}

public JPanel createDisplayNbRowPanel() {
JPanel p = new JPanel();
p.add(new JLabel("number of row : "));
NbRowView nbRowView = new NbRowView(model);
p.add(nbRowView);
p.setBorder(new LineBorder(Color.red));

return p;
}

public int getNbRow() {
return nbRow;
}

public void setNbRow(int nbRow) {
this.nbRow = nbRow;
}

class NbRowView extends JLabel implements TableModelListener {

private MyTableModel model;

public NbRowView(MyTableModel model) {
this.model = model;
model.addTableModelListener(this);
}

@Override
public void tableChanged(TableModelEvent arg0) {
int nbOfRow = model.getRowCount();
this.setText(nbOfRow + "");
DrawPanel drawPanel = new DrawPanel();
drawPanel.setNumberOfArrows(nbOfRow);

}

}

}

DrawTwoSameDrawPanel 类

public class DrawTwoSameDrawPanel extends JPanel {

private DrawPanel drawPanel_1 = new DrawPanel();
private DrawPanel drawPanel_2 = new DrawPanel();

public DrawTwoSameDrawPanel() {
super();
this.setBorder(new LineBorder(Color.green));
this.add(drawPanel_1);
this.add(drawPanel_2);
}

}

最佳答案

I want to draw x lines in my gui when i have x rows in my jtable.

看看你之前的问题:How to synchronize two view in Java Swing您已经很好地实现了 DrawPanelpaintComponent() 方法,但是您仍然需要将其与表模型解耦。正如其他开发人员所说,您的代码庞大且复杂,您需要更好地组织它:

  1. 创建一个可重复使用的面板,如this answer所示
  2. 当您创建表模型的新实例时,请附加新的 TableModelListener调用 setNumberOfArrows() 方法,该方法将根据您的意愿重新绘制面板。

例如:

class GuiClass {

private DrawPanel drawPanel;

public void createAndShowGui() {
drawPanel = new DrawPanel();
MyTableModel model = new MyTableModel();
model.addTableModelListener(new TableModelListener() { // Anonymous class
@Override
public void tableChanged(TableModelEvent e) {
if(e.getType() == TableModelEvent.INSERT || e.getType() == TableModelEvent.DELETE) {
int rows = ((TableModel)e.getSource()).getRowCount();
drawPanel.setNumberOfArrows(rows);
}
}
});
// rest of your GUI code here
}
}

注意:Java 提供了一种良好的机制来避免创建过多的类来实现接口(interface)或从其他类扩展:Anonymous Classes

关于您的 table 型号,请注意它看起来像 DefaultTableModel差不多,所以您可能想使用该类而不是创建自己的类(当然如果它符合您的要求)。

此外,执行getColumnClass()方法永远不应该查找数据来检索值,您应该能够返回正确的类,因为您知道表模型将包含哪种数据。

最后,您的 addRow(Object[][] row) 方法没有意义,因为您可能想向表模型添加一行,而不是行和列。我认为它应该是 addRow(Object[] row) (注意单维数组作为参数)。话虽如此,它应该看起来像这样:

public void addRow(Object[] row) {
// add row here
int rowIndex = data.length;
fireTableRowsInserted(rowIndex, rowIndex);
}

参见AbstractTableModel API :

关于java - 如何在我的图形用户界面中绘制准确的行数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24929011/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com