gpt4 book ai didi

java - 对多个 JPanel 使用 CardLayout 并且不显示任何内容

转载 作者:行者123 更新时间:2023-12-02 03:08:24 27 4
gpt4 key购买 nike

我正在制作一个简单(且虚假)的计算机功耗计算器。我使用卡片布局来放置多个面板,但是当我运行它时,只有一个小窗口不显示任何内容。这是我的长代码,我将它们全部放在一个类中。

package my.Project;

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.DecimalFormat;
import javax.swing.*;


public class MainProject extends JFrame {

CardLayout cl;

int motherboardP, oddP, hddP, ssdP, fanP, cpuP, gpuP, monitorP, hoursint;
int ramNum, hddNum, ssdNum, fanNum, gpuNum;
double ramP, totalP, powerPerYear;
public float tariff = (float) 0.2180;
public float costRM;

JPanel mainPage, secondPage, thirdPage, results;
//JPanel panelCont;
JLabel title, icon, motherboard, ram, numram,
numssd, numhdd, odd, numfan, cpu, gpu,
numgpu, monitor, hours, outage, peryear,
costyear, watt, kwatt, rm;

JButton start, exit1, exit2, nxt1, bck1, nxt2, bck2, done, bck3, tips, calc;

JTextField numRam, numSSD, numHDD, numFan, numGpu, hoursUse,
outagePC, perYear, cost;

JComboBox mboardBox, ramBox, oddBox, cpuBox, gpuBox,
monitorBox;

public MainProject() {
initComponents();
}

public void initComponents() {

setTitle("Power Consumption Calculator");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(500, 400);

Container contentPane = getContentPane();

contentPane.setLayout(cl);

/*
panelCont = new JPanel();
cl = new CardLayout();
*/

//contentPane.add(panelCont);

/*
panelCont.setLayout(cl);
panelCont.add(mainPage,"1");
panelCont.add(secondPage,"2");
panelCont.add(thirdPage,"3");
panelCont.add(results,"4");
cl.show(panelCont,"1");
*/

mainPage = new JPanel();

secondPage = new JPanel();

thirdPage = new JPanel();

results = new JPanel();

cl.show(contentPane,"1");

contentPane.add("1", mainPage);
contentPane.add("2", secondPage);
contentPane.add("3", thirdPage);
contentPane.add("4", results);

title.setFont(new Font("Tahoma", Font.BOLD, 18));
title.setText("Computer Power Consumption Calculator");
motherboard.setText("Motherboard:");
ram.setText("RAM:");
numram.setText("No. of RAM Sticks:");
numssd.setText("Num. of SSD:");
numhdd.setText("Num. of HDD:");
odd.setText("Optical Disk Drive:");
numfan.setText("Num. of Fans:");
cpu.setText("CPU:");
gpu.setText("GPU:");
numgpu.setText("Num. of GPU:");
monitor.setText("Monitor:");
hours.setText("Hours of usage/day:");
outage.setText("PC Power Outage:");
peryear.setText("Per year:");
costyear.setText("Cost/year");
watt.setText("Watts (W)");
kwatt.setText("kiloWatts(kW)");
rm.setText("RM");

icon.setIcon(new javax.swing.ImageIcon(getClass().getResource("/my/Project/pclogo.png")));

BorderLayout mainL = new BorderLayout();
mainPage.setLayout(mainL);
mainPage.add(title, BorderLayout.PAGE_START);
mainPage.add(icon, BorderLayout.CENTER);
mainPage.add(start, BorderLayout.SOUTH);
mainPage.add(exit1,BorderLayout.SOUTH);

GridLayout secondL = new GridLayout(0,2);
secondPage.setLayout(secondL);
secondPage.add(motherboard);
secondPage.add(mboardBox);
secondPage.add(ram);
secondPage.add(ramBox);
secondPage.add(numram);
secondPage.add(numRam);
secondPage.add(numssd);
secondPage.add(numSSD);
secondPage.add(numhdd);
secondPage.add(numHDD);
secondPage.add(odd);
secondPage.add(oddBox);
secondPage.add(numfan);
secondPage.add(numFan);
secondPage.add(nxt1);
secondPage.add(bck1);

GridLayout thirdL = new GridLayout(0,2);
thirdPage.setLayout(thirdL);
thirdPage.add(cpu);
thirdPage.add(cpuBox);
thirdPage.add(gpu);
thirdPage.add(gpuBox);
thirdPage.add(numGpu);
thirdPage.add(monitor);
thirdPage.add(monitorBox);
thirdPage.add(hours);
thirdPage.add(hoursUse);
thirdPage.add(nxt2);
thirdPage.add(bck2);

GridLayout resultL = new GridLayout(0,3);
results.setLayout(resultL);
results.add(outage);
results.add(outagePC);
results.add(watt);
results.add(peryear);
results.add(perYear);
results.add(kwatt);
results.add(costyear);
results.add(cost);
results.add(rm);
results.add(calc);
results.add(bck3);
results.add(exit2);
results.add(tips);

start.setText("Start");
start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//startActionPerformed(evt);
cl.show(contentPane,"2");
}
});

exit1.setText("Exit");
exit1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
// exit1ActionPerformed(evt);
System.exit(0);
}
});

exit2.setText("Exit");
exit2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
// exit2ActionPerformed(evt);
System.exit(0);
}
});

nxt1.setText("Next >>");
nxt1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
// nxt1ActionPerformed(evt);
cl.show(contentPane,"3");
ramNum = Integer.parseInt(numRam.getText());
ssdNum = Integer.parseInt(numSSD.getText());
hddNum = Integer.parseInt(numHDD.getText());
fanNum = Integer.parseInt(numFan.getText());

JComboBox cb1 = (JComboBox)evt.getSource();
String mboard = (String)cb1.getSelectedItem();
if("Average".equals(mboard)) {
motherboardP = 32;
}
if("High End".equals(mboard)) {
motherboardP = 60;
}

JComboBox cb2 = (JComboBox)evt.getSource();
String ramType = (String)cb2.getSelectedItem();
if("DDDR1".equals(ramType)) {
ramP = 5;
}
if("DDR2".equals(ramType)) {
ramP = 4;
}
if("DDR3".equals(ramType)) {
ramP = 2.5;
}

JComboBox cb3 = (JComboBox)evt.getSource();
String typeODD = (String)cb3.getSelectedItem();
if("DVD".equals(typeODD)) {
oddP = 22;
}
if("BluRay".equals(typeODD)) {
oddP = 27;
}


}
});

nxt2.setText("Next >>");
nxt2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
// nxt2ActionPerformed(evt);
cl.show(contentPane,"4");

gpuNum = Integer.parseInt(numGpu.getText());
hoursint = Integer.parseInt(hoursUse.getText());

JComboBox cb4 = (JComboBox)evt.getSource();
int inCPU = (Integer)cb4.getSelectedIndex();
switch(inCPU) {
case 0:
cpuP = 89;
break;
case 1:
cpuP = 119;
break;
case 2:
cpuP = 127;
break;
case 3:
cpuP = 125;
break;
case 4:
cpuP = 33;
break;
case 5:
cpuP = 98;
break;

}

JComboBox cb5 = (JComboBox)evt.getSource();
int nvGPU = (Integer)cb5.getSelectedIndex();

switch(nvGPU) {
case 0:
gpuP = 295;
break;
case 1:
gpuP = 100;
break;
case 2:
gpuP = 250;
break;
case 3:
gpuP = 150;
break;
case 4:
gpuP = 105;
break;
case 5:
gpuP = 275;
break;
}

JComboBox cb6 = (JComboBox)evt.getSource();
int mSize = (Integer)cb6.getSelectedIndex();
if(mSize == 0) {
monitorP = 20;
}
if(mSize == 1) {
monitorP = 28;
}
if(mSize == 2) {
monitorP = 50;
}

}
});

bck1.setText("<< Back");
bck1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
// bck1ActionPerformed(evt);
cl.show(contentPane,"1");
}
});

bck2.setText("<< Back");
bck2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
// bck2ActionPerformed(evt);
cl.show(contentPane,"2");
}
});

bck3.setText("<< Back");
bck3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
// bck3ActionPerformed(evt);
cl.show(contentPane,"3");
}
});

tips.setText("Tips on saving energy");
tips.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//tipsActionPerformed(evt);
try {

String url ="http://www.toptenreviews.com/computers/articles/10-computer-energy-saving-tips-go-green/";

Desktop dt = Desktop.getDesktop();
URI uri = new URI(url);
dt.browse(uri.resolve(uri));
}

catch (URISyntaxException | IOException e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
}
});

calc.setText("Calculate");
calc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
// calcActionPerformed(evt);

gpuP *= gpuNum;
ramP *= ramNum;
hddP = hddNum * 5;
ssdP = ssdNum * 3;
fanP = fanNum * 4;

totalP = motherboardP + cpuP + gpuP + ramP +
hddP + ssdP + monitorP + fanP;
powerPerYear = totalP * hoursint * 365;

costRM = (float) ((powerPerYear / 1000) * tariff);

DecimalFormat df = new DecimalFormat("#.##");
df.format(costRM);

String outage = Double.toString(totalP);
String ppy = Double.toString(powerPerYear);
String price = Float.toString(costRM);

outagePC.setText(outage);
perYear.setText(ppy);
cost.setText(price);
}
});

mboardBox.setModel(new DefaultComboBoxModel(new String[] { "Average", "High End" }));
mboardBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
// mboardBoxActionPerformed(evt);
JComboBox cb = (JComboBox)evt.getSource();
String mboard = (String)cb.getSelectedItem();
if("Average".equals(mboard)) {
motherboardP = 32;
}
if("High End".equals(mboard)) {
motherboardP = 60;
}
}
});

ramBox.setModel(new DefaultComboBoxModel(new String[] { "DDR1", "DDR2", "DDR3" }));
ramBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//ramBoxActionPerformed(evt);
JComboBox cb = (JComboBox)evt.getSource();
String ramType = (String)cb.getSelectedItem();
if("DDDR1".equals(ramType)) {
ramP = 5;
}
if("DDR2".equals(ramType)) {
ramP = 4;
}
if("DDR3".equals(ramType)) {
ramP = 2.5;
}
}
});

oddBox.setModel(new DefaultComboBoxModel(new String[] { "DVD", "BluRay" }));
oddBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//oddBoxActionPerformed(evt);
JComboBox cb = (JComboBox)evt.getSource();
String typeODD = (String)cb.getSelectedItem();
if("DVD".equals(typeODD)) {
oddP = 22;
}
if("BluRay".equals(typeODD)) {
oddP = 27;
}
}
});

cpuBox.setModel(new DefaultComboBoxModel(new String[] { "intel Core i3", "intel Core i5", "intel Core i7", "AMD FX 4350", "AMD Sempron 64 3500", "AMD Athlon 64 FX-62" }));
cpuBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//cpuBoxActionPerformed(evt);
JComboBox cb = (JComboBox)evt.getSource();
int inCPU = (Integer)cb.getSelectedIndex();
switch(inCPU) {
case 0:
cpuP = 89;
break;
case 1:
cpuP = 119;
break;
case 2:
cpuP = 127;
break;
case 3:
cpuP = 125;
break;
case 4:
cpuP = 33;
break;
case 5:
cpuP = 98;
break;

}

}
});

gpuBox.setModel(new DefaultComboBoxModel(new String[] { "NVidia GTX 500", "NVidia GTX 700", "NVidia GTX Titan", "AMD Radeon HD 2900", "AMD Radeon HD 3870", "AMD Radeon R9 Fury X" }));
gpuBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//gpuBoxActionPerformed(evt);
JComboBox cb = (JComboBox)evt.getSource();
int nvGPU = (Integer)cb.getSelectedIndex();

switch(nvGPU) {
case 0:
gpuP = 295;
break;
case 1:
gpuP = 100;
break;
case 2:
gpuP = 250;
break;
case 3:
gpuP = 150;
break;
case 4:
gpuP = 105;
break;
case 5:
gpuP = 275;
break;
}
}

});

monitorBox.setModel(new DefaultComboBoxModel(new String[] { "17\"-19\"", "20\"-22\"", "24\"-30\"" }));
monitorBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//monitorBoxActionPerformed(evt);
JComboBox cb = (JComboBox)evt.getSource();
int mSize = (Integer)cb.getSelectedIndex();
if(mSize == 0) {
monitorP = 20;
}
if(mSize == 1) {
monitorP = 28;
}
if(mSize == 2) {
monitorP = 50;
}
}

});


}

public static void go() {

JFrame frame = new JFrame();
frame.setVisible(true);
}

public static void main(String[] args) {

MainProject.go();

}

}

我应该在哪里修复代码以及我的代码中实现了多少错误的 Java 编程实践?

最佳答案

首先...

在将 CardLayout 应用到容器之前以及添加任何组件之前,您需要创建一个 CardLayout 实例...

    cl = new CardLayout();
contentPane.setLayout(cl);

第二...

您应该使用add(Component, Object)方法

contentPane.add(mainPage, "1");

第三

public static void go() {

JFrame frame = new JFrame();
frame.setVisible(true);
}

什么也不做,只是创建一个空的框架,里面什么也没有。实际使用 MainProject 可能会有所帮助,因为它扩展自 JFrame

第四...

不要直接从 JFrame 扩展,您不会向类添加任何新功能,它会将您锁定在单一用例中,并会导致像您所遇到的问题。

相反,请考虑从 JPanel 开始......

public class MainProject extends JPanel {

(ps-此更改可能会导致其他编译器错误,我不会在此处尝试修复这些错误)

然后简单地创建一个 JFrame 的新实例并向其中添加您的组件...

public static void go() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new MainProject());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

第五

您可能想仔细看看...

还有...

了解更多想法以及如何更好地管理 CardLayout

您可能还想查看How to Use Tabbed Panes替代方案

关于java - 对多个 JPanel 使用 CardLayout 并且不显示任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57008898/

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