Java 向JFrame添加扩展JTable 我想做什么:
我创建了一个扩展JTable的table类。我的目标是在获得用户输入后显示在互联网上找到的图像(目前我只是使用硬编码URL进行测试)。一列应该是ImageIcon类,第二列是String。当使用测试类时,JTable(未扩展)被正确绘制,但我无法显示扩展的JTable。我想我在TeamTable类中遗漏了一些东西,但我不知道它是什么,以及为什么测试类(位于本文末尾)实际工作 主要问题是: 为什么Test.java可以正确显示带有ImageIcon的JTable,但我的RoundRobin.java+TeamTable.java根本不显示任何内容 守则:Java 向JFrame添加扩展JTable 我想做什么:,java,swing,jtable,jframe,imageicon,Java,Swing,Jtable,Jframe,Imageicon,我创建了一个扩展JTable的table类。我的目标是在获得用户输入后显示在互联网上找到的图像(目前我只是使用硬编码URL进行测试)。一列应该是ImageIcon类,第二列是String。当使用测试类时,JTable(未扩展)被正确绘制,但我无法显示扩展的JTable。我想我在TeamTable类中遗漏了一些东西,但我不知道它是什么,以及为什么测试类(位于本文末尾)实际工作 主要问题是: 为什么Test.java可以正确显示带有ImageIcon的JTable,但我的RoundRobin.jav
public类TeamTable扩展了JTable{
公共静态最终字符串[]columnNames={“团队徽标”,“团队”};
公共静态最终对象[][]数据=新对象[1][2];//出于测试目的,设置为[1][2],最终值[8][2]
公共团队表(){
JTable table=新的JTable();
configureTeamTable(table);//重写getColumnClass,设置模型
}//遍历所有团队并分配名称和徽标
公共静态void configureTeamTable(JTable表){
DefaultTableModel=新的DefaultTableModel(数据、列名){
@凌驾
公共类getColumnClass(int列){
返回getValueAt(0,列).getClass();
}
};
表2.setModel(model);
表1.设置行高(50);
对于(int i=0;i<1;i++){//出于测试目的,设置为1,最终值为8
ImageIcon currentTeamLogo=新的ImageIcon(RoundRobin.getTeam(i).getLogo());
字符串currentTeamName=RoundRobin.getTeam(i).getName();
数据[i][0]=currentTeamLogo;
数据[i][1]=当前团队名称;
}
}
}
这是扩展的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
公共类团队{
私有字符串名称;
私人形象标识;
私人ArrayList玩家;
公共团队(字符串名称){
this.name=名称;
}
公共团队(){}
公共无效搜索img(){
试一试{
URL=新URL(“http://www.wearjersey.com/product_images/uploaded_images/milanlogo.jpg");
this.logo=ImageIO.read(url);
}捕获(IOE异常){
系统输出打印项次(“IOe”);
}
}
public void resizeLogo(){
Image tempImg=this.getLogo();
tempImg=tempImg.getScaledInstance(30,30,java.awt.Image.SCALE\u-SMOOTH);
setLogo(tempImg);
}
}//仅删除下面的getter和setter,以缩短代码
这是团队类,目前它总是搜索相同的图像,并且可以将其大小调整为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();
}
}
}
公共类循环{
私有JFrame;
私有静态数组列表团队;
私有静态int-teamCounter=0;
最终整数高度=400;//1024
最终整数长度=600;//1920
公共静态void main(字符串[]args){
RoundRobin rr=新的RoundRobin();
rr.paintStuff();
rr.initFirstUI();
}
公共物品{
frame=新的JFrame();
框架尺寸(长度、高度);
frame.getContentPane().setLayout(新的MigLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}//初始化、设置大小等。
public void initFirstUI(){//创建UI的第一部分,该部分稍后将被组梯形图替换
团队=新的ArrayList();
添加(新JLabel(“团队”),“包裹,对齐中心”);
JButton addTeamBut=新JButton(“添加团队”);
frame.add(addTeamBut);
addTeamBut.addActionListener(新的AddTeamAct());
刷新();
}
public void refresh(){//只是一个revaildate+refresh方法
frame.revalidate();
frame.repaint();
}
公共静态团队getTeam(int索引){
返回团队。获取(索引);
}
类AddTeamAct实现ActionListener{
@凌驾
已执行的公共无效行动(行动事件ae){
如果(teamCounter<1){//出于测试目的设置为1,则最终值为8
String name=JOptionPane.showInputDialog(“团队名称”);
团队当前团队=新团队(名称);
currentTeam.searchImg();
currentTeam.resizeLogo();
添加(当前团队);
teamCounter++;
}
如果(teamCounter==1){//出于测试目的设置为1,则最终值为8
TeamTable a=新的TeamTable();
框架.添加(a);
}
刷新();
}
}
}
==================工作的测试类,Test.java和tje TeamTable.java之间的主要区别是什么?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;
}
}
公共类测试{
私有JFrame;
专用JTable团队表;
私人形象标识;
公共静态void main(字符串[]args){
测试=新测试();
test.paintStuff();
test.addTab();
}
公共物品{
frame=新的JFrame();
框架设置尺寸(800600);
frame.getContentPane().setLayout(新的MigLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
公共void addTab(){
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;
}
}