简体   繁体   English

来自Jframe的Jpanel中的doClick()和SetText()

[英]doClick() and SetText() in Jpanel from Jframe

I'm new in Java programming. 我是Java编程的新手。 I developed a GUI in Netbeans (unfortunately using drag and drop). 我在Netbeans中开发了一个GUI(不幸的是使用拖放)。

I created a JFrame and two JPanel (using CardLayout). 我创建了一个JFrame和两个JPanel(使用CardLayout)。

JFrame: JFrame:

private void formKeyPressed(java.awt.event.KeyEvent evt) {                                
    int key=evt.getKeyCode();

    switch (key) {

            case 105: 
                UpdatePanel update=new UpdatePanel(105);
                break;

            case 97:
                CardLayout showPanel = (CardLayout) panelCard.getLayout();
                showPanel.show(panelCard, "Incasso");
                break;

            case 99:
                showPanel = (CardLayout) panelCard.getLayout();
                showPanel.show(panelCard, "Selezione");
                break;

So when I press keycode 99, it shows the panelCard "Selezione" , and with keycode 97, panelCard "Incasso" 因此,当我按下键码99时,它将显示panelCard "Selezione" ,而键码为97时,则显示panelCard "Incasso"

This works properly. 这可以正常工作。

With keycode 105 it calls the class UpdatePanel . 使用键代码105,它将调用类UpdatePanel

public class UpdatePanel {
  public UpdatePanel(int key){
    switch (key) {
        case 105:
            Selezione variabile= new Selezione();

            variabile.fascia.setText("io");

            break;
    }
}

The scope of this class is to update the component inside the panels. 此类的范围是更新面板内的组件。 In this sample I try to update the text field "fascia" that is a component inside the panel "Selezione" . 在此示例中,我尝试更新文本"fascia" ,它是面板"Selezione"内的一个组件。

I compiled the files without errors. 我编译文件没有错误。 When I press the button (code 105) the class UpdatePanel is instantiated successfully, but the fieldText "fascia" is not updated in the panel. 当我按下按钮(代码105)时,类UpdatePanel被成功实例化,但是fieldText "fascia"在面板中未更新。

What am I doing wrong ? 我究竟做错了什么 ?

I tried to some modifications, but most of the code was made by the drag and drop wizard, so I do not have direct access. 我尝试了一些修改,但是大多数代码是由拖放向导完成的,因此我没有直接访问权限。

UpdatePanel does not look like a good class name. UpdatePanel看起来不是一个很好的类名。 It is better good for a method name, to say doSomething . doSomething来表示方法名称更好。 If you are intending to write a class responsible for updating panel, better name it as PanelUpdater . 如果打算编写一个负责更新面板的类,最好将其命名为PanelUpdater

You could have written the case 105 as 您可以将案例105写成

case 105: 
       fascia.setText("io");
       break;

Or if you needed to do this with a class, say PanelUpdater , 或者,如果您需要对一个类执行此操作,请说PanelUpdater

public class PanelUpdater {
  public PanelUpdater (int key, JTextField field){
    switch (key) {
        case 105:
            field.setText("io");
            break;
    }
}

and

case 105: 
                PanelUpdater panelUpdater = new PanelUpdater (105, fascia);
                break;

But I do not recommend this approach of creating an Object unnecessarily. 但是我不建议这种不必要的创建Object方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM