我的小应用程序的主要目的是通过按键盘上的“Enter”按钮更新Panel中的Panel,它只在一个Frame中。
以下是课程: 主要 - 启动应用; MainFrame - 是具有更新方法的Frame; PanelWithNoThreads - 是Panel的类,它是在框架初始化时创建的; PanelWithNoFramesContr - 正在控制 PanelWithNoThreads 所发生的一切。 PanelWithThread 和 PanelWithThreadContr - 是屏幕及其控制器,与之前的配件完全相同。
我会把代码放在这篇文章的末尾。
所以,PROBlEM是......
那么,如何解决这个问题。
class Main
public class Main {
static MainFrame mainFrame;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
mainFrame = new MainFrame();
}
}
class MainFrame:
public class MainFrame extends JFrame implements KeyListener{
JPanel theMainPanel = new JPanel();
PanelWithThreadContr pwntContr;
static String idOfPanel = "empty_panel";
public MainFrame(){
setSize(800,600);
setExtendedState(MAXIMIZED_BOTH);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
theMainPanel.setBackground(Color.BLACK);
theMainPanel.setForeground(Color.WHITE);
add(theMainPanel);
System.out.println("LOG/MainFrame : added the empty Panel");
//default change to the Panel with no Threads
PanelWithNoFramesContr ntpc = new PanelWithNoFramesContr();
changeThePane(ntpc.pwnt, ntpc.getId(), ntpc);
}
private void changeThePane(JPanel inputPanel, String id, KeyListener keyListener){
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
remove(theMainPanel);
theMainPanel = inputPanel;
add(theMainPanel);
addKeyListener(keyListener);
repaint();
theMainPanel.validate();
theMainPanel.repaint();
theMainPanel.setVisible(true);
}
});
}
public void implementThePane(String id){
if(id.contains("one_thread")){
pwntContr = new PanelWithThreadContr();
changeThePane(pwntContr.pwt, pwntContr.id, pwntContr);
}
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
}
class PanelWithNoThreads:
public class PanelWithNoThreads extends javax.swing.JPanel {
/**
* Creates new form PanelWithNoThreads
*/
public PanelWithNoThreads() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
HelloLabel = new javax.swing.JLabel();
HelloLabel.setFont(new java.awt.Font("Osaka", 0, 48)); // NOI18N
HelloLabel.setText("Hello!!!");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(HelloLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 532, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(158, 158, 158)
.addComponent(HelloLabel)
.addContainerGap(164, Short.MAX_VALUE))
);
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JLabel HelloLabel;
// End of variables declaration
public void setColorsToHelloLabel(Color backColor, Color foreColor){
HelloLabel.setBackground(backColor);
HelloLabel.setForeground(foreColor);
}
}
class PanelWithNoThreadsContr:
public class PanelWithNoThreadsContr extends Controller implements KeyListener{
PanelWithNoThreads pwnt = new PanelWithNoThreads();
private String id = "no_threads";
public PanelWithNoThreadsContr() {
//settings
pwnt.setBackground(Color.BLACK);
pwnt.setForeground(Color.WHITE);
pwnt.setColorsToHelloLabel(Color.BLACK, Color.GREEN);
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
System.out.println("LOG/no_thread : Pressed 'ENTER'");
if(e.getKeyCode() == KeyEvent.VK_ENTER){
Main.mainFrame.implementThePane("one_thread");
}
}
@Override
public void keyReleased(KeyEvent e) {
}
/**
* @return the id
*/
public String getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(String id) {
this.id = id;
}
}
class PanelWithThread:
public class PanelWithThread extends javax.swing.JPanel {
/**
* Creates new form PanelWithThread
*/
public PanelWithThread() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
threadedLabel = new javax.swing.JLabel();
threadedLabel.setFont(new java.awt.Font("ArcadeClassic", 0, 36)); // NOI18N
threadedLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
threadedLabel.setText("NO_TEXT");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(threadedLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(threadedLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 276, Short.MAX_VALUE)
.addContainerGap())
);
}// </editor-fold>
// Variables declaration - do not modify
public javax.swing.JLabel threadedLabel;
// End of variables declaration
}
class PanelWithThreadContr:
public class PanelWithThreadContr extends Controller implements KeyListener{
PanelWithThread pwt = new PanelWithThread();
String id = "one_thread";
public PanelWithThreadContr() {
System.out.println("LOG/one_thread : creating");
pwt.setBackground(Color.BLACK);
pwt.setForeground(Color.WHITE);
pwt.threadedLabel.setText("*****");
pwt.threadedLabel.setForeground(Color.GREEN);
pwt.threadedLabel.setBackground(Color.BLACK);
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER){
}
}
}
答案 0 :(得分:0)
您需要在每个自定义JPanel的构造函数中调用super()。