依然是JTabbedPanel选项卡的问题,改了半天了还是没改出来啊,只改出了删除当前选项卡!
怎么修改以下程序才能让每个选项卡都能下面输入信息,上面收到信息呢!?(以下代码只有最后一个选项卡能实现,其他都不行,虽然知道原因,但还是改不动)publicclassTe...
怎么修改以下程序才能让每个选项卡都能下面输入信息,上面收到信息呢!?
(以下代码只有最后一个选项卡能实现,其他都不行,虽然知道原因,但还是改不动)
public class Test extends JFrame implements ActionListener {
private JPanel panel;
private JTabbedPane tabbedPane = new JTabbedPane(SwingConstants.TOP);
private JTextArea output;
private JTextArea input;
private JButton sendBtn;
private JButton offlineBtn;
private JButton addBtn;
public Test(){
setup();
}
public void setup(){
setSize(450, 600);
addBtn = new JButton("添加选项卡");
addBtn.addActionListener(this);
add(addBtn, BorderLayout.WEST);
add(tabbedPane, BorderLayout.CENTER);
setVisible(true);
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void createTab() {
panel = new JPanel();
panel.setLayout(null);
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new BorderLayout());
output = new JTextArea();
output.setEditable(false);
output.setLineWrap(true);
output.setWrapStyleWord(true);
centerPanel.add(new JScrollPane(output), BorderLayout.CENTER);
JPanel southPanel = new JPanel();
southPanel.setLayout(new BorderLayout());
input = new JTextArea();
input.setLineWrap(true);
input.setWrapStyleWord(true);
southPanel.add(new JScrollPane(input), BorderLayout.CENTER);
JPanel bottomPanel = new JPanel();
sendBtn = new JButton("发送");
sendBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if((input.getText()).equals("")){
} else {
output.append(input.getText() + "\n");
input.setText("");
}
}
});
bottomPanel.add(sendBtn);
offlineBtn = new JButton("退出");
offlineBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
deleteTab();
tabbedPane.revalidate();
tabbedPane.repaint();
}
});
bottomPanel.add(offlineBtn);
southPanel.add(bottomPanel, BorderLayout.SOUTH);
centerPanel.setSize(430, 366);
centerPanel.setLocation(0, 0);
southPanel.setSize(430, 171);
southPanel.setLocation(0, 368);
panel.add(centerPanel);
panel.add(southPanel);
panel.setOpaque(true);
tabbedPane.addTab("ds"++tabbedPane.getTabCount(), panel);
}
private void deleteTab() {
if (tabbedPane.getTabCount() > 0)
tabbedPane.removeTabAt(tabbedPane.getSelectedIndex());
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == addBtn){
createTab();
tabbedPane.revalidate();
tabbedPane.repaint();
}
}
public static void main(String[] args){
new Test();
}
} 展开
(以下代码只有最后一个选项卡能实现,其他都不行,虽然知道原因,但还是改不动)
public class Test extends JFrame implements ActionListener {
private JPanel panel;
private JTabbedPane tabbedPane = new JTabbedPane(SwingConstants.TOP);
private JTextArea output;
private JTextArea input;
private JButton sendBtn;
private JButton offlineBtn;
private JButton addBtn;
public Test(){
setup();
}
public void setup(){
setSize(450, 600);
addBtn = new JButton("添加选项卡");
addBtn.addActionListener(this);
add(addBtn, BorderLayout.WEST);
add(tabbedPane, BorderLayout.CENTER);
setVisible(true);
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void createTab() {
panel = new JPanel();
panel.setLayout(null);
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new BorderLayout());
output = new JTextArea();
output.setEditable(false);
output.setLineWrap(true);
output.setWrapStyleWord(true);
centerPanel.add(new JScrollPane(output), BorderLayout.CENTER);
JPanel southPanel = new JPanel();
southPanel.setLayout(new BorderLayout());
input = new JTextArea();
input.setLineWrap(true);
input.setWrapStyleWord(true);
southPanel.add(new JScrollPane(input), BorderLayout.CENTER);
JPanel bottomPanel = new JPanel();
sendBtn = new JButton("发送");
sendBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if((input.getText()).equals("")){
} else {
output.append(input.getText() + "\n");
input.setText("");
}
}
});
bottomPanel.add(sendBtn);
offlineBtn = new JButton("退出");
offlineBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
deleteTab();
tabbedPane.revalidate();
tabbedPane.repaint();
}
});
bottomPanel.add(offlineBtn);
southPanel.add(bottomPanel, BorderLayout.SOUTH);
centerPanel.setSize(430, 366);
centerPanel.setLocation(0, 0);
southPanel.setSize(430, 171);
southPanel.setLocation(0, 368);
panel.add(centerPanel);
panel.add(southPanel);
panel.setOpaque(true);
tabbedPane.addTab("ds"++tabbedPane.getTabCount(), panel);
}
private void deleteTab() {
if (tabbedPane.getTabCount() > 0)
tabbedPane.removeTabAt(tabbedPane.getSelectedIndex());
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == addBtn){
createTab();
tabbedPane.revalidate();
tabbedPane.repaint();
}
}
public static void main(String[] args){
new Test();
}
} 展开
2个回答
展开全部
你的程序我帮你改完了,主要是把两个JTextArea的定义移到createTab()函数中,并加final修饰符,你看看吧。
完整的程序如下:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Test extends JFrame implements ActionListener {
private JPanel panel;
private JTabbedPane tabbedPane = new JTabbedPane(SwingConstants.TOP);
private JButton sendBtn;
private JButton offlineBtn;
private JButton addBtn;
public Test(){
setup();
}
public void setup(){
setSize(450, 600);
addBtn = new JButton("添加选项卡");
addBtn.addActionListener(this);
add(addBtn, BorderLayout.WEST);
add(tabbedPane, BorderLayout.CENTER);
setVisible(true);
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void createTab() {
panel = new JPanel();
panel.setLayout(null);
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new BorderLayout());
final JTextArea output = new JTextArea(); //这里改了一下
output.setEditable(false);
output.setLineWrap(true);
output.setWrapStyleWord(true);
centerPanel.add(new JScrollPane(output), BorderLayout.CENTER);
JPanel southPanel = new JPanel();
southPanel.setLayout(new BorderLayout());
final JTextArea input = new JTextArea(); //这里改闭游埋了一下轿蚂
input.setLineWrap(true);
input.setWrapStyleWord(true);
southPanel.add(new JScrollPane(input),BorderLayout.CENTER);
JPanel bottomPanel = new JPanel();
sendBtn = new JButton("发送"磨宴);
sendBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if((input.getText()).equals("")){
} else {
output.append(input.getText() + "\n");
input.setText("");
}
}
});
bottomPanel.add(sendBtn);
offlineBtn = new JButton("退出");
offlineBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
deleteTab();
tabbedPane.revalidate();
tabbedPane.repaint();
}
});
bottomPanel.add(offlineBtn);
southPanel.add(bottomPanel, BorderLayout.SOUTH);
centerPanel.setSize(430, 366);
centerPanel.setLocation(0, 0);
southPanel.setSize(430, 171);
southPanel.setLocation(0, 368);
panel.add(centerPanel);
panel.add(southPanel);
panel.setOpaque(true);
tabbedPane.addTab("ds"+tabbedPane.getTabCount(), panel);
}
private void deleteTab() {
if (tabbedPane.getTabCount() > 0)
tabbedPane.removeTabAt(tabbedPane.getSelectedIndex());
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == addBtn){
createTab();
tabbedPane.revalidate();
tabbedPane.repaint();
}
}
public static void main(String[] args){
new Test();
}
}
完整的程序如下:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Test extends JFrame implements ActionListener {
private JPanel panel;
private JTabbedPane tabbedPane = new JTabbedPane(SwingConstants.TOP);
private JButton sendBtn;
private JButton offlineBtn;
private JButton addBtn;
public Test(){
setup();
}
public void setup(){
setSize(450, 600);
addBtn = new JButton("添加选项卡");
addBtn.addActionListener(this);
add(addBtn, BorderLayout.WEST);
add(tabbedPane, BorderLayout.CENTER);
setVisible(true);
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void createTab() {
panel = new JPanel();
panel.setLayout(null);
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new BorderLayout());
final JTextArea output = new JTextArea(); //这里改了一下
output.setEditable(false);
output.setLineWrap(true);
output.setWrapStyleWord(true);
centerPanel.add(new JScrollPane(output), BorderLayout.CENTER);
JPanel southPanel = new JPanel();
southPanel.setLayout(new BorderLayout());
final JTextArea input = new JTextArea(); //这里改闭游埋了一下轿蚂
input.setLineWrap(true);
input.setWrapStyleWord(true);
southPanel.add(new JScrollPane(input),BorderLayout.CENTER);
JPanel bottomPanel = new JPanel();
sendBtn = new JButton("发送"磨宴);
sendBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if((input.getText()).equals("")){
} else {
output.append(input.getText() + "\n");
input.setText("");
}
}
});
bottomPanel.add(sendBtn);
offlineBtn = new JButton("退出");
offlineBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
deleteTab();
tabbedPane.revalidate();
tabbedPane.repaint();
}
});
bottomPanel.add(offlineBtn);
southPanel.add(bottomPanel, BorderLayout.SOUTH);
centerPanel.setSize(430, 366);
centerPanel.setLocation(0, 0);
southPanel.setSize(430, 171);
southPanel.setLocation(0, 368);
panel.add(centerPanel);
panel.add(southPanel);
panel.setOpaque(true);
tabbedPane.addTab("ds"+tabbedPane.getTabCount(), panel);
}
private void deleteTab() {
if (tabbedPane.getTabCount() > 0)
tabbedPane.removeTabAt(tabbedPane.getSelectedIndex());
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == addBtn){
createTab();
tabbedPane.revalidate();
tabbedPane.repaint();
}
}
public static void main(String[] args){
new Test();
}
}
更多追问追答
追问
(⊙o⊙)…这么简单,汗……让我鼓捣了那么久。
这个……那个……有个小小的问题,就是那个output组件,除了在该createTab()方法外,现在就没办法获得啦,那外部信息还怎么弄上去呀???(类似QQ吧,不同选项卡内的output显示的是不同的信息,外部也可以再往里面添加上信息)
追答
QQ的外部信息是通过Socket传进传出的,不是通过外部访问不同选项卡内的output来传进去的。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询