gpt4 book ai didi

java - 为什么我的 JTable 接管了我的其他 JComponent

转载 作者:行者123 更新时间:2023-12-04 05:45:09 25 4
gpt4 key购买 nike

我已经尝试解决这个问题有一段时间了,我已经检查了文档(如果您注意到表格代码来自那里),但我无法弄清楚。我是否使用了错误的容器,还是需要对其进行尺寸标注?如果是这样,如何?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Invest extends JFrame {

public static void main(String[] args) {
demolayout frame = new demolayout();
frame.setTitle("Financial Calculator");

JPanel toppanel = new JPanel(new GridLayout(3, 3, 2, 2));
frame.add(toppanel, BorderLayout.NORTH);

String[] columnNames = {"Entry", "Year", "Year", "Year", "Year"};

Object[][] data = {{"Period Ending:", "2011", "Snowboarding", new Integer(5), new Boolean(false)},
{"Values in Millions of $", "12-31", "Rowing", new Integer(3), new Boolean(true)},
{"Revenue:", "46,542", "Knitting", new Integer(2), new Boolean(false)},
{"Revenue Other:", "-", "Speed reading", new Integer(20), new Boolean(true)},
{"Total Revenue:", "46,542", "Speed reading", new Integer(20), new Boolean(true)},
{"Cost of Revenue:", "18,216", "Speed reading", new Integer(20), new Boolean(true)},
{"Gross Profit:", "28,326", "Speed reading", new Integer(20), new Boolean(true)},
{"Selling Gen. Admin Expenses:", "12,111", "Speed reading", new Integer(20), new Boolean(true)},
{"Research & Development:", "-", "Speed reading", new Integer(20), new Boolean(true)},
{"Depreciation/Amortization:", "-", "Speed reading", new Integer(20), new Boolean(true)},
{"Total Operating Expense:", "154", "Speed reading", new Integer(20), new Boolean(true)},
{"Operating Income:", "35,810", "Speed reading", new Integer(20), new Boolean(true)},
{"Interest Income (Exp.), Net NonOp:", "10,732", "Speed reading", new Integer(20), new Boolean(true)},
{"Interest/Invest Inc. Non-Operating:", "White", "Speed reading", new Integer(20), new Boolean(true)},
{"Interest Inc. Net Non-Operating:", "White", "Speed reading", new Integer(20), new Boolean(true)},
{"Gain/Loss on Sale of Assets:", "White", "Speed reading", new Integer(20), new Boolean(true)},
{"Other, Net:", "White", "Speed reading", new Integer(20), new Boolean(true)},
{"Net Income Before Taxes:", "White", "Speed reading", new Integer(20), new Boolean(true)},
{"Income Tax - Total:", "White", "Speed reading", new Integer(20), new Boolean(true)},
{"Minority Interest:", "Brown", "Pool", new Integer(10), new Boolean(false)},
{"Equity In Affiliates:", "Brown", "Pool", new Integer(10), new Boolean(false)},
{"Accounting Change:", "Brown", "Pool", new Integer(10), new Boolean(false)},
{"Discontinued Operations:", "Brown", "Pool", new Integer(10), new Boolean(false)},
{"Extraordinary Items:", "Brown", "Pool", new Integer(10), new Boolean(false)},
{"Net Income:", "Brown", "Pool", new Integer(10), new Boolean(false)}};

JTable table = new JTable(data, columnNames);
JScrollPane scrollPane = new JScrollPane(table);
toppanel.add(scrollPane);

JPanel bottompanel = new JPanel(new FlowLayout());
bottompanel.setBackground(new Color(150, 150, 150));

JButton compute = new JButton("Compute Ratios");
bottompanel.add(compute);
frame.add(bottompanel, BorderLayout.SOUTH);


frame.setSize(800, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

enter image description here

最佳答案

看起来不错添加到框架的 BorderLayout.CENTER .

此外,如果存在现有的原始方法或工厂方法,请不要创建新对象。

附录:工厂方法是什么意思?

好问题; Integer. valueOf()是典型的static factory方法。如 API 中所述并在 Joshua Bloch 的 Item 1: Consider static factory methods instead of constructors 中讨论的那样,该方法缓存频繁请求的值。

table

import java.awt.*;
import javax.swing.*;

public class Invest extends JFrame {

public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("Financial Calculator");

JPanel toppanel = new JPanel(new GridLayout(3, 3, 2, 2));
frame.add(toppanel, BorderLayout.NORTH);

String[] columnNames = {"Entry", "Year", "Year", "Year", "Year"};

Object[][] data = {{"Period Ending:", "2011", "Snowboarding", Integer.valueOf(5), false},
{"Values in Millions of $", "12-31", "Rowing", Integer.valueOf(3), true},
{"Revenue:", "46,542", "Knitting", Integer.valueOf(2), false},
{"Revenue Other:", "-", "Speed reading", Integer.valueOf(20), true},
{"Total Revenue:", "46,542", "Speed reading", Integer.valueOf(20), true},
{"Cost of Revenue:", "18,216", "Speed reading", Integer.valueOf(20), true},
{"Gross Profit:", "28,326", "Speed reading", Integer.valueOf(20), true},
{"Selling Gen. Admin Expenses:", "12,111", "Speed reading", Integer.valueOf(20), true},
{"Research & Development:", "-", "Speed reading", Integer.valueOf(20), true},
{"Depreciation/Amortization:", "-", "Speed reading", Integer.valueOf(20), true},
{"Total Operating Expense:", "154", "Speed reading", Integer.valueOf(20), true},
{"Operating Income:", "35,810", "Speed reading", Integer.valueOf(20), true},
{"Interest Income (Exp.), Net NonOp:", "10,732", "Speed reading", Integer.valueOf(20), true},
{"Interest/Invest Inc. Non-Operating:", "White", "Speed reading", Integer.valueOf(20), true},
{"Interest Inc. Net Non-Operating:", "White", "Speed reading", Integer.valueOf(20), true},
{"Gain/Loss on Sale of Assets:", "White", "Speed reading", Integer.valueOf(20), true},
{"Other, Net:", "White", "Speed reading", Integer.valueOf(20), true},
{"Net Income Before Taxes:", "White", "Speed reading", Integer.valueOf(20), true},
{"Income Tax - Total:", "White", "Speed reading", Integer.valueOf(20), true},
{"Minority Interest:", "Brown", "Pool", Integer.valueOf(10), false},
{"Equity In Affiliates:", "Brown", "Pool", Integer.valueOf(10), false},
{"Accounting Change:", "Brown", "Pool", Integer.valueOf(10), false},
{"Discontinued Operations:", "Brown", "Pool", Integer.valueOf(10), false},
{"Extraordinary Items:", "Brown", "Pool", Integer.valueOf(10), false},
{"Net Income:", "Brown", "Pool", Integer.valueOf(10), false}};

JTable table = new JTable(data, columnNames);
JScrollPane scrollPane = new JScrollPane(table);
frame.add(scrollPane, BorderLayout.CENTER);

JPanel bottompanel = new JPanel(new FlowLayout());
bottompanel.setBackground(new Color(150, 150, 150));

JButton compute = new JButton("Compute Ratios");
bottompanel.add(compute);
frame.add(bottompanel, BorderLayout.SOUTH);

frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

关于java - 为什么我的 JTable 接管了我的其他 JComponent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10778716/

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