gpt4 book ai didi

java - 将扩展 JTable 添加到 JFrame

转载 作者:行者123 更新时间:2023-12-01 18:14:52 25 4
gpt4 key购买 nike

我想做的事情:

我创建了一个扩展 JTable 的表类。我的目标是在获得用户输入后显示在互联网上找到的图像(目前我只是使用硬编码的 URL 进行测试)。一列应该是 ImageIcon 类,第二列是 String。使用测试类时,JTable(未扩展)被正确绘制,但是我根本无法显示扩展的表。我认为我在 TeamTable 类中遗漏了一些内容,但我无法弄清楚它是什么,以及为什么 Test 类(位于帖子末尾)实际上有效。

主要问题:

为什么Test.java可以正确显示带有ImageIcon的JTable,但我的RoundRobin.java + TeamTable.java根本不显示任何内容?

代码:

public class TeamTable extends JTable{
public static final String[] columnNames = {"Team Logo", "Team"};
public static final Object[][] data = new Object[1][2]; // For test purposes set to [1][2], final value [8][2]

public TeamTable(){
JTable table = new JTable();
configureTeamTable(table); // Override the getColumnClass, set the model
} // Loop through all teams and assing the names and logos

public static void configureTeamTable(JTable table){
DefaultTableModel model = new DefaultTableModel(data, columnNames){
@Override
public Class<?> getColumnClass(int column){
return getValueAt(0, column).getClass();
}
};
table.setModel(model);
table.setRowHeight(50);
for(int i = 0 ; i < 1 ; i ++){ // For test purposes set to 1, final value 8
ImageIcon currentTeamLogo = new ImageIcon(RoundRobin.getTeam(i).getLogo());
String currentTeamName = RoundRobin.getTeam(i).getName();
data[i][0] = currentTeamLogo;
data[i][1] = currentTeamName;
}
}
}

这是扩展的 JTable 类,我正在创建一个新的 JTable,重写 getColumnClass 方法并使用从主类中的用户收集的输入设置两列。

public class Team {
private String name;
private Image logo;
private ArrayList<String> players;


public Team(String name){
this.name = name;
}
public Team(){}

public void searchImg(){
try{
URL url = new URL("http://www.wearjersey.com/product_images/uploaded_images/milanlogo.jpg");
this.logo = ImageIO.read(url);
}catch(IOException e){
System.out.println("IOe");
}
}

public void resizeLogo(){
Image tempImg = this.getLogo();
tempImg = tempImg.getScaledInstance(30,30, java.awt.Image.SCALE_SMOOTH);
setLogo(tempImg);
}
} //only GETTERS and SETTERS below, removed to keep the code shorter

这是Team类,目前它总是搜索相同的图像,并且可以将其大小调整为30x30px大小。主要方法如下:

public class RoundRobin {

private JFrame frame;
private static ArrayList<Team> teams;
private static int teamCounter = 0;

final int HEIGHT = 400; //1024
final int LENGTH = 600; //1920


public static void main(String[] args) {
RoundRobin rr = new RoundRobin();
rr.paintStuff();
rr.initFirstUI();
}


public void paintStuff() {
frame = new JFrame();

frame.setSize(LENGTH, HEIGHT);
frame.getContentPane().setLayout(new MigLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);
} // initiates, sets size etc.

public void initFirstUI() { // creates the first part of the UI, which will later be replaced by group ladder
teams = new ArrayList();
frame.add(new JLabel("Teams"), "wrap, align center");
JButton addTeamBut = new JButton("Add a team");
frame.add(addTeamBut);
addTeamBut.addActionListener(new AddTeamAct());

refresh();
}


public void refresh() { // just a revaildate + refresh method
frame.revalidate();
frame.repaint();
}

public static Team getTeam(int index) {
return teams.get(index);
}


class AddTeamAct implements ActionListener {

@Override
public void actionPerformed(ActionEvent ae) {
if(teamCounter < 1){ // For test purposes set to 1, final value 8
String name = JOptionPane.showInputDialog("Name of the team");

Team currentTeam = new Team(name);
currentTeam.searchImg();
currentTeam.resizeLogo();

teams.add(currentTeam);
teamCounter++;
}
if(teamCounter == 1){ // For test purposes set to 1, final value 8
TeamTable a = new TeamTable();
frame.add(a);
}
refresh();
}
}
}

============== 工作Test类,Test.java和TeamTable.java之间的主要区别是什么? Test.java 正确显示 JFrame。

public class Test {
private JFrame frame;
private JTable teamTable;
private Image logo;

public static void main(String[] args) {
Test test = new Test();
test.paintStuff();
test.addTab();
}




public void paintStuff(){
frame = new JFrame();

frame.setSize(800, 600);
frame.getContentPane().setLayout(new MigLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);
}


public void addTab(){
searchImg();
resizeLogo();
ImageIcon a = new ImageIcon(logo);
String[] columnNames = {"Team"};
Object[][] data = new ImageIcon[8][1];
data[0][0] = a;
for(int i = 0 ; i < 8 ; i ++){
data[i][0] = a;
}
DefaultTableModel model = new DefaultTableModel(data, columnNames);

teamTable = new JTable(model){
public Class getColumnClass(int column){
return getValueAt(0, column).getClass();
}
};

teamTable.setRowHeight(50);

frame.add(teamTable);
frame.revalidate();
frame.repaint();
}

public void searchImg(){
try{
URL url = new URL("http://www.wearjersey.com/product_images/uploaded_images/milanlogo.jpg");
this.logo = ImageIO.read(url);
}catch(IOException e){
System.out.println("IOException");
}
}

public void resizeLogo(){
Image tempImg = this.logo;
tempImg = tempImg.getScaledInstance(30,30, java.awt.Image.SCALE_SMOOTH);
logo = tempImg;
}
}

感谢您的见解。

最佳答案

TeamTable 都扩展了 JTable 并具有 JTable 的实例。这很可能就是问题所在。

顺便说一句,代码试图实现的目标似乎在​​工厂方法中实现比通过扩展 JTable 更好。

关于java - 将扩展 JTable 添加到 JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30286244/

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