我现在在网上看了大约2个小时。我需要完成的是在另一个类中使用按钮的方法,更改整个程序的框架窗口的标题。所以基本上我想做到这一点,以便客户可以将setTitle更改为他的商业名称。这是我到目前为止所涉及的两个类的代码。
Options.java
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Options extends JPanel {
JLabel nameLabel;
JTextField nameField;
JButton sNn;
public Options()
{
nameLabel = new JLabel();
nameLabel.setText("Change Company Name:");
nameField = new JTextField(22);
sNn = getTitleChangeButton();
ExitButton exitButton = new ExitButton();
this.add(nameLabel);
this.add(nameField);
this.add(sNn);
this.add(exitButton.getExitButton());
}
public JButton getTitleChangeButton()
{
JButton ChangeTitle;
ChangeTitle = new JButton("Change Company Title");
ChangeTitle.setMnemonic('C');
ChangeTitle.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String title;
}
});
return ChangeTitle;
}
}
这是名为tabbedPane的第二个类。这是我想成为超类的类,并通过按钮更改了setTitle。
TabbedPane.java
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
//import poolCalculation.PoolVolumeCalc;
//import poolCalculation.GenTab;
//import poolCalculation.TempCalc;
import poolCalculation.Options;
public class TabbedPane extends JFrame
{
public TabbedPane()
{
setTitle("Gardening INC.");
JTabbedPane Tabs = new JTabbedPane();
getContentPane().add(Tabs);
JPanel jp1 = new JPanel();
//Tabs.addTab("Pool Volume Calculator", new PoolVolumeCalc());
//Tabs.addTab("General Tab", new GenTab());
//Tabs.addTab("Temperature Calculation", new TempCalc());
Tabs.addTab("Options", new Options());
getContentPane().add(Tabs);
setSize(400,300);
setVisible(true);
}
public static void main(String[] args) {
TabbedPane test = new TabbedPane();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
对于我的生活,当用户点击选项中的按钮时,我无法弄清楚如何为框架更改标题。这是我到目前为止尝试过的,我尝试制作setTitle(title)并在TabbedPane的主要部分中创建一个字符串,然后使用get方法从选项中获取文本,但这不起作用。或者我做错了。我尝试将按钮设为自己的类,但却无法读取文本字段。
答案 0 :(得分:3)
以下是演示如何动态更改标题的示例:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class NormalFrame extends JFrame
{
private static final long serialVersionUID = 1L;
public NormalFrame()
{
super("The title");
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container contentPane = getContentPane();
contentPane.add(new CustomPanel(this));
pack();
setLocationRelativeTo(null);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
new NormalFrame().setVisible(true);
}
});
}
}
class CustomPanel extends JPanel
{
private static final long serialVersionUID = 1L;
private JFrame mainFrame;
private JLabel lbl;
private JTextField txt;
private JButton btn;
private ActionListener listener = new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
mainFrame.setTitle(txt.getText());
}
};
public CustomPanel(JFrame mainFrame)
{
this.mainFrame = mainFrame;
lbl = new JLabel("The new title: ");
txt = new JTextField(10);
btn = new JButton("Change Title");
btn.addActionListener(listener);
setLayout(new BorderLayout());
add(lbl, BorderLayout.LINE_START);
add(txt, BorderLayout.CENTER);
add(btn, BorderLayout.LINE_END);
}
}