GridBagLayout 不保持秩序
GridBagLayout Not Keeping Order
我正在尝试按特定顺序制作一个简单的两列视图。
它应该是这样的:
顶行有两个静态 headers,每个下方每列有 5 个面板
然而,我得到的结果是左侧空白 table,所有详细信息在右侧水平排列。问题是详细信息是从互联网上实时加载的,每 8 秒刷新一次,所以我需要在数据刷新时更新每个单元格(这就是为什么我不使用 GridLayout
)
代码无限循环(不断调出up-to-date数据直到退出)
我无法预先初始化所有 JPanels
,因为从网上获取每个数据点至少需要 6 秒。
你能帮我整理一下订单吗?谢谢
import NeuralNet.NeuralNet;
import javax.swing.*;
import java.awt.*;
public class MWindow {
public MWindow(String[] c){
JFrame mainFr = new JFrame("Current Predictions");
mainFr.setSize(800, 800);
mainFr.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
mainFr.setVisible(true);
JPanel p = new JPanel(new GridBagLayout());
GridBagConstraints cns = new GridBagConstraints();
cns.fill = GridBagConstraints.HORIZONTAL;
cns.insets = new Insets(5,5,15,15);
mainFr.add(p);
JTextArea man = new JTextArea("MANUFACTURING 5");
cns.gridx = 0;
cns.gridy = 0;
cns.weightx = 1.0;
cns.weighty = 1.0;
cns.fill = GridBagConstraints.BOTH;
p.add(man, cns);
JTextArea inter = new JTextArea("INTERNET 5");
cns.gridx = 1;
cns.gridy = 0;
cns.weightx = 1.0;
cns.weighty = 1.0;
cns.fill = GridBagConstraints.BOTH;
p.add(inter, cns);
JPanel aapl = new JPanel();
JPanel msft = new JPanel();
JPanel intc = new JPanel();
JPanel ibm = new JPanel();
JPanel tsla = new JPanel();
JPanel fb = new JPanel();
JPanel goog = new JPanel();
JPanel yhoo = new JPanel();
JPanel twtr = new JPanel();
JPanel amzn = new JPanel();
p.setBackground(Color.white);
mainFr.setBackground(Color.white);
while (true) {
for (String cmp : c) {
JPanel stkPanel = new JPanel();
stkPanel.setBackground(Color.white);
if (!(cmp.equals("INTER5") || cmp.equals("MANU5") || cmp.equals("ALL"))) {
Asset a = new Asset(cmp);
NeuralNet n = Functions.loadNN(cmp);
NeuralNet nA = Functions.loadNN("ALL");
NeuralNet n5;
if (cmp.equals("MSFT") || cmp.equals("AAPL") || cmp.equals("INTC") || cmp.equals("IBM") || cmp.equals("TSLA")) {
n5 = Functions.loadNN("MANU5");
} else if (cmp.equals("TWTR") || cmp.equals("YHOO") || cmp.equals("GOOG") || cmp.equals("FB") || cmp.equals("AMZN")) {
n5 = Functions.loadNN("INTER5");
} else {
System.out.println("ERROR");
n5 = n;
}
double pred = n.PredictRows(Functions.formatData(a));
double pred5 = n5.PredictRows(Functions.formatData(a));
double predA = nA.PredictRows(Functions.formatData(a));
JTextArea stkPred = new JTextArea();
stkPred.setText("Stock: " + cmp +
"\nCurrent Price: " + "$" + a.getCurrPrice().toString() +
"\nPrediction 1: " + Double.toString(pred) +
"\nPrediction 2: " + Double.toString(pred5) +
"\nPrediction 3: " + Double.toString(predA));
stkPred.setFont(new Font("Helvetica", Font.BOLD, 15));
int pr = Functions.calcBS(pred, pred5, predA);
JTextArea act = new JTextArea();
if (pr == -1) {
act.setText("SELL");
act.setForeground(Color.red);
} else if (pr == 1) {
act.setText("BUY");
act.setForeground(Color.green);
} else {
act.setText("HOLD");
act.setForeground(Color.orange);
}
act.setFont(new Font("Helvetica", Font.BOLD, 15));
stkPanel.add(stkPred);
stkPanel.add(act);
switch (cmp) {
case "MSFT":
msft.removeAll();
msft.add(stkPanel);
msft.revalidate();
msft.repaint();
cns.gridx = 0;
cns.gridy = 2;
cns.weightx = 1.0;
cns.weighty = 1.0;
cns.fill = GridBagConstraints.BOTH;
p.add(msft, cns);
case "AAPL":
aapl.removeAll();
aapl.add(stkPanel);
aapl.revalidate();
aapl.repaint();
cns.gridx = 0;
cns.gridy = 1;
cns.weightx = 1.0;
cns.weighty = 1.0;
cns.fill = GridBagConstraints.BOTH;
p.add(aapl, cns);
case "INTC":
intc.removeAll();
intc.add(stkPanel);
intc.revalidate();
intc.repaint();
cns.gridx = 0;
cns.gridy = 3;
p.add(intc, cns);
case "IBM":
ibm.removeAll();
ibm.add(stkPanel);
ibm.revalidate();
ibm.repaint();
cns.gridx = 0;
cns.gridy = 4;
p.add(ibm, cns);
case "TSLA":
tsla.removeAll();
tsla.add(stkPanel);
tsla.revalidate();
tsla.repaint();
cns.gridx = 0;
cns.gridy = 5;
p.add(tsla, cns);
case "TWTR":
twtr.removeAll();
twtr.add(stkPanel);
twtr.revalidate();
twtr.repaint();
cns.gridx = 1;
cns.gridy = 4;
p.add(twtr, cns);
case "FB":
fb.removeAll();
fb.add(stkPanel);
fb.revalidate();
fb.repaint();
cns.gridx = 1;
cns.gridy = 1;
p.add(fb, cns);
case "AMZN":
amzn.removeAll();
amzn.add(stkPanel);
amzn.revalidate();
amzn.repaint();
cns.gridx = 1;
cns.gridy = 5;
p.add(amzn, cns);
case "GOOG":
goog.removeAll();
goog.add(stkPanel);
goog.revalidate();
goog.repaint();
cns.gridx = 1;
cns.gridy = 2;
p.add(goog, cns);
case "YHOO":
yhoo.removeAll();
yhoo.add(stkPanel);
yhoo.revalidate();
yhoo.repaint();
cns.gridx = 1;
cns.gridy = 3;
p.add(yhoo, cns);
}
p.add(stkPanel);
p.revalidate();
}
}
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
首先,我强烈建议您查看 Concurrency in Swing,因为您违反了 Swing 的单线程性质。
然后我建议您查看 Worker Threads and SwingWorker 以获得可能的解决方案。
您的代码中发生了很多事情,添加、删除和创建了很多东西,这简直令人困惑。
您通常更新的信息不是动态的,数据是动态的,但呈现方式不是动态的。
因此,我建议改为创建一个自定义 class 来封装您要显示的信息,这成为模型,然后创建一个自定义 class 可以以您想要的格式显示信息。
然后我会创建一些这样的组件,并以任何你想要的方式将它们放置在容器中,也许使用 Map
来绑定它们(这样你就可以查找给定数据源的组件当它改变时)。
然后,我会简单地将 data/model 传递给适当的组件,当它发生变化时,它可以简单地更新显示。
根据我从您的代码中可以看出的情况,JTable
以及一系列 JLabel
s/JTextField
s 可能会继续下去,从而使它变得容易得多从
更新 UI
这也会让add/remove数据源
更容易
我正在尝试按特定顺序制作一个简单的两列视图。
它应该是这样的:
顶行有两个静态 headers,每个下方每列有 5 个面板
然而,我得到的结果是左侧空白 table,所有详细信息在右侧水平排列。问题是详细信息是从互联网上实时加载的,每 8 秒刷新一次,所以我需要在数据刷新时更新每个单元格(这就是为什么我不使用 GridLayout
)
代码无限循环(不断调出up-to-date数据直到退出)
我无法预先初始化所有 JPanels
,因为从网上获取每个数据点至少需要 6 秒。
你能帮我整理一下订单吗?谢谢
import NeuralNet.NeuralNet;
import javax.swing.*;
import java.awt.*;
public class MWindow {
public MWindow(String[] c){
JFrame mainFr = new JFrame("Current Predictions");
mainFr.setSize(800, 800);
mainFr.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
mainFr.setVisible(true);
JPanel p = new JPanel(new GridBagLayout());
GridBagConstraints cns = new GridBagConstraints();
cns.fill = GridBagConstraints.HORIZONTAL;
cns.insets = new Insets(5,5,15,15);
mainFr.add(p);
JTextArea man = new JTextArea("MANUFACTURING 5");
cns.gridx = 0;
cns.gridy = 0;
cns.weightx = 1.0;
cns.weighty = 1.0;
cns.fill = GridBagConstraints.BOTH;
p.add(man, cns);
JTextArea inter = new JTextArea("INTERNET 5");
cns.gridx = 1;
cns.gridy = 0;
cns.weightx = 1.0;
cns.weighty = 1.0;
cns.fill = GridBagConstraints.BOTH;
p.add(inter, cns);
JPanel aapl = new JPanel();
JPanel msft = new JPanel();
JPanel intc = new JPanel();
JPanel ibm = new JPanel();
JPanel tsla = new JPanel();
JPanel fb = new JPanel();
JPanel goog = new JPanel();
JPanel yhoo = new JPanel();
JPanel twtr = new JPanel();
JPanel amzn = new JPanel();
p.setBackground(Color.white);
mainFr.setBackground(Color.white);
while (true) {
for (String cmp : c) {
JPanel stkPanel = new JPanel();
stkPanel.setBackground(Color.white);
if (!(cmp.equals("INTER5") || cmp.equals("MANU5") || cmp.equals("ALL"))) {
Asset a = new Asset(cmp);
NeuralNet n = Functions.loadNN(cmp);
NeuralNet nA = Functions.loadNN("ALL");
NeuralNet n5;
if (cmp.equals("MSFT") || cmp.equals("AAPL") || cmp.equals("INTC") || cmp.equals("IBM") || cmp.equals("TSLA")) {
n5 = Functions.loadNN("MANU5");
} else if (cmp.equals("TWTR") || cmp.equals("YHOO") || cmp.equals("GOOG") || cmp.equals("FB") || cmp.equals("AMZN")) {
n5 = Functions.loadNN("INTER5");
} else {
System.out.println("ERROR");
n5 = n;
}
double pred = n.PredictRows(Functions.formatData(a));
double pred5 = n5.PredictRows(Functions.formatData(a));
double predA = nA.PredictRows(Functions.formatData(a));
JTextArea stkPred = new JTextArea();
stkPred.setText("Stock: " + cmp +
"\nCurrent Price: " + "$" + a.getCurrPrice().toString() +
"\nPrediction 1: " + Double.toString(pred) +
"\nPrediction 2: " + Double.toString(pred5) +
"\nPrediction 3: " + Double.toString(predA));
stkPred.setFont(new Font("Helvetica", Font.BOLD, 15));
int pr = Functions.calcBS(pred, pred5, predA);
JTextArea act = new JTextArea();
if (pr == -1) {
act.setText("SELL");
act.setForeground(Color.red);
} else if (pr == 1) {
act.setText("BUY");
act.setForeground(Color.green);
} else {
act.setText("HOLD");
act.setForeground(Color.orange);
}
act.setFont(new Font("Helvetica", Font.BOLD, 15));
stkPanel.add(stkPred);
stkPanel.add(act);
switch (cmp) {
case "MSFT":
msft.removeAll();
msft.add(stkPanel);
msft.revalidate();
msft.repaint();
cns.gridx = 0;
cns.gridy = 2;
cns.weightx = 1.0;
cns.weighty = 1.0;
cns.fill = GridBagConstraints.BOTH;
p.add(msft, cns);
case "AAPL":
aapl.removeAll();
aapl.add(stkPanel);
aapl.revalidate();
aapl.repaint();
cns.gridx = 0;
cns.gridy = 1;
cns.weightx = 1.0;
cns.weighty = 1.0;
cns.fill = GridBagConstraints.BOTH;
p.add(aapl, cns);
case "INTC":
intc.removeAll();
intc.add(stkPanel);
intc.revalidate();
intc.repaint();
cns.gridx = 0;
cns.gridy = 3;
p.add(intc, cns);
case "IBM":
ibm.removeAll();
ibm.add(stkPanel);
ibm.revalidate();
ibm.repaint();
cns.gridx = 0;
cns.gridy = 4;
p.add(ibm, cns);
case "TSLA":
tsla.removeAll();
tsla.add(stkPanel);
tsla.revalidate();
tsla.repaint();
cns.gridx = 0;
cns.gridy = 5;
p.add(tsla, cns);
case "TWTR":
twtr.removeAll();
twtr.add(stkPanel);
twtr.revalidate();
twtr.repaint();
cns.gridx = 1;
cns.gridy = 4;
p.add(twtr, cns);
case "FB":
fb.removeAll();
fb.add(stkPanel);
fb.revalidate();
fb.repaint();
cns.gridx = 1;
cns.gridy = 1;
p.add(fb, cns);
case "AMZN":
amzn.removeAll();
amzn.add(stkPanel);
amzn.revalidate();
amzn.repaint();
cns.gridx = 1;
cns.gridy = 5;
p.add(amzn, cns);
case "GOOG":
goog.removeAll();
goog.add(stkPanel);
goog.revalidate();
goog.repaint();
cns.gridx = 1;
cns.gridy = 2;
p.add(goog, cns);
case "YHOO":
yhoo.removeAll();
yhoo.add(stkPanel);
yhoo.revalidate();
yhoo.repaint();
cns.gridx = 1;
cns.gridy = 3;
p.add(yhoo, cns);
}
p.add(stkPanel);
p.revalidate();
}
}
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
首先,我强烈建议您查看 Concurrency in Swing,因为您违反了 Swing 的单线程性质。
然后我建议您查看 Worker Threads and SwingWorker 以获得可能的解决方案。
您的代码中发生了很多事情,添加、删除和创建了很多东西,这简直令人困惑。
您通常更新的信息不是动态的,数据是动态的,但呈现方式不是动态的。
因此,我建议改为创建一个自定义 class 来封装您要显示的信息,这成为模型,然后创建一个自定义 class 可以以您想要的格式显示信息。
然后我会创建一些这样的组件,并以任何你想要的方式将它们放置在容器中,也许使用 Map
来绑定它们(这样你就可以查找给定数据源的组件当它改变时)。
然后,我会简单地将 data/model 传递给适当的组件,当它发生变化时,它可以简单地更新显示。
根据我从您的代码中可以看出的情况,JTable
以及一系列 JLabel
s/JTextField
s 可能会继续下去,从而使它变得容易得多从
这也会让add/remove数据源
更容易