亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使一個類中的按鈕影響另一個類中的文本區域?

如何使一個類中的按鈕影響另一個類中的文本區域?

嗶嗶one 2022-09-01 19:35:54
請幫助我了解這是如何運作的。例如,我很難理解如何在一個類中更改文本,而該類位于同一包的另一個類中。我做了一個簡單的應用程序,只是為了在這里問一個問題,我需要這個更大的學校項目,我需要實現這個來與多個班級一起工作。JButtonJTextArea當我把所有東西放在同一個類中時,它可以工作,但我需要在單獨的類中使用它。下面是簡單的代碼。import javax.swing.*;import java.awt.event.*;import java.awt.*;class Button extends JPanel {    private JButton button;    private Panel panel;    public Button() {        button = new JButton("BUTTON");        panel = new Panel();        add(button);        button.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent e) {                JButton clicked = (JButton) e.getSource();                String input = clicked.getText();                panel.setTextArea(input);                //System.out.println(input);            }        });    }}class Panel extends JPanel {    private JTextArea textArea;    public Panel() {        setLayout(new BorderLayout());        textArea = new JTextArea();        add(textArea, BorderLayout.CENTER);    }    public JTextArea getTextArea() {        return textArea;    }    void setTextArea(String text) {        this.textArea.setText(text);    }}public class Java extends JFrame {    private Button dugme;    private JFrame frame;    private Panel panel;    public Java() {        frame = new JFrame();        dugme = new Button();        panel = new Panel();        //super("test");        frame.setLayout(new BorderLayout());        frame.setTitle("test");        frame.setSize(300, 400);        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        frame.setVisible(true);        frame.add(dugme, BorderLayout.NORTH);        frame.add(panel, BorderLayout.CENTER);    }    public static void main(String[] args) {        Java app = new Java();    }}我希望動作監聽器改變面板中的文本,系統輸出工作,所以監聽器監聽按鈕,但我無法改變文本區域中的文本。
查看完整描述

1 回答

?
MM們

TA貢獻1886條經驗 獲得超2個贊

正如@XtremeBaumer已經提到的,你有兩個不同的類實例。您需要刪除 secode 一個。Panel


public class Button extends JPanel {

    private JButton button;

    private Panel panel;

    public Button(Panel panel) { // we need already created instance of panel here.

        this.panel = panel;

        button = new JButton("BUTTON");

        // panel = new Panel(); <-- this line must be deleted.

        // ...

    }

}

public class Java extends JFrame {

    private Button dugme;

    private JFrame frame;

    private Panel panel;

    public Java(){

        frame = new JFrame();

        panel = new Panel();

        dugme = new Button(panel);

        // ...

    }

}

請同時更換線路


add(textArea, BorderLayout.CENTER);


add(new JScrollPane(textArea), BorderLayout.CENTER);

這允許您在文本大于文本 ara 大小時獲取 scrool 條。


這是您重新設計的示例


import javax.swing.*;

import java.awt.event.*;

import java.awt.*;


class Button extends JPanel {


    private JButton button;

    private Panel panel;


    public Button(Panel panel) {

        this.panel = panel;

        button = new JButton("BUTTON");

        add(button);

        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                JButton clicked = (JButton) e.getSource();

                String input = clicked.getText();

                panel.setTextArea(input);

                //System.out.println(input);

            }

        });

    }

}


class Panel extends JPanel {


    private JTextArea textArea;


    public Panel() {

        setLayout(new BorderLayout());

        textArea = new JTextArea();

        add(new JScrollPane(textArea), BorderLayout.CENTER);

    }


    public JTextArea getTextArea() {

        return textArea;

    }


    void setTextArea(String text) {

        this.textArea.setText(text);

    }

}


public class Java extends JFrame {


    private Button dugme;

    private JFrame frame;

    private Panel panel;


    public Java() {

        frame = new JFrame();

        panel = new Panel();

        dugme = new Button(panel);

        //super("test");

        frame.setLayout(new BorderLayout());

        frame.setTitle("test");

        frame.setSize(300, 400);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setVisible(true);

        frame.add(dugme, BorderLayout.NORTH);

        frame.add(panel, BorderLayout.CENTER);

    }


    public static void main(String[] args) {

        Java app = new Java();

    }

}


查看完整回答
反對 回復 2022-09-01
  • 1 回答
  • 0 關注
  • 101 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號