gpt4 book ai didi

java - 想要我的 JButton 返回上一个文本区域

转载 作者:行者123 更新时间:2023-12-01 11:51:24 24 4
gpt4 key购买 nike

我正在尝试创建一个 GUI,在程序打开时为您提供提示。我不确定如何添加文本,然后创建允许我转到下一个文本或返回到上一个文本的按钮。我不确定我应该使用什么类型的actionListener。

import java.awt.*;
import javax.swing.*;


public class HelpfulHints extends JFrame{

private JTextArea area; //This is my textarea to display hints
private JPanel panel;
private JButton close, previous, next; //Three buttons to operate program

public HelpfulHints(){

super("Tip of the Day");
setLayout(new BorderLayout());

panel = new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.CENTER));

//Create first button
close= new JButton("Close");
add(close);

//Create second button
previous = new JButton("Previous");
add(previous);

//Create third button
next = new JButton("Next");
add(next);

//Add buttons to panel
panel.add(close);
panel.add(previous);
panel.add(next);

//Keep buttons to south of panel
add(panel, BorderLayout.SOUTH);

//Create ButtonHandler for button event handling
ButtonHandler handler = new ButtonHandler();
close.addActionListener(handler);
previous.addActionListener(handler);
next.addActionListener(handler);

//Create the helpful hint area on the screen
area = new JTextArea();
area.setEditable(true);
area.setLayout(new FlowLayout(FlowLayout.CENTER));

add(area, BorderLayout.CENTER);

setVisible(true);

}

//Inner class for button event handling
private class ButtonHandler implements ActionListener{

//handle Button event
@Override
public void actionPerformed(ActionEvent e){


}
}
}

最佳答案

我已经根据您的需要更新了监听器的内容。

private JTextArea area; //This is my textarea to display hints
private JPanel panel;
private JButton close, previous, next; //Three buttons to operate program
private List<String> tips = new ArrayList<String>();
private int displayedTipIndex = 0;

public HelpfulHints(){
super("Tip of the Day");
// set up your tips
tips.add("First Tip");
tips.add("Second Tip");
tips.add("Third Tip");

// ... your other code stays the same...

//Create the helpful hint area on the screen
area = new JTextArea();
area.setEditable(true);
area.setLayout(new FlowLayout(FlowLayout.CENTER));
// set first tip
area.setText(tips.get(displayedTipIndex));
add(area, BorderLayout.CENTER);

// disable previous button when we start
previous.setEnabled(false);

//
setVisible(true);
}

//Inner class for button event handling
private class ButtonHandler implements ActionListener{

//handle Button event
@Override
public void actionPerformed(ActionEvent e){
if(e.getSource()==next){
displayedTipIndex++;
area.setText(tips.get(displayedTipIndex));
// disable the next button if no more tips
if(displayedTipIndex>=tips.size()-1){
next.setEnabled(false);
}
// re-enable previpous button
previous.setEnabled(true);
}
if(e.getSource()==previous){
displayedTipIndex--;
area.setText(tips.get(displayedTipIndex));
/// disable the previous button if no more tips
if(displayedTipIndex<0){
previous.setEnabled(false);
}
// re-enable next button
next.setEnabled(true);
}
if(e.getSource()==close){
System.exit(0);
}
}
}

关于java - 想要我的 JButton 返回上一个文本区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28799604/

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