Swing中选项卡不显示怎么回事?

陌小然灬 2019-01-15 04:42:50
我编的选项卡不显示是为什么?

package send;


import java.awt.*;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import java.awt.FlowLayout;

public class send
{
public static int n = 0;//定义一个全局变量。
public static Frame f;
public static TextField rateText;
public static Label rateLabel;
public static Label ipListLabel;
public static TextArea ipText;
public static Button btStart;
public static Button btEnd;
public static TextArea debugText;
public static JTabbedPane jtp;
public static JPanel jp1;
public static JPanel jp2;

public static double rate;//发送速率
public static ArrayList<String> receiveIp = new ArrayList<String>();//接收者ip集合




public static void init(){
f = new Frame("send");
f.setLayout(new FlowLayout());
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;
screenSize.setSize(screenWidth/2, screenHeight/2);
f.setPreferredSize(screenSize);

f.setBounds(600,100,400,200);
f.setLocationRelativeTo(null);
f.setLayout(null);

//UDP
jtp = new JTabbedPane();
jp1 = new JPanel();
jp2 = new JPanel();
jtp.add("UDP",jp1);
jtp.add("TCP",jp2);
f.add(jtp);



rateLabel = new Label("Send Rate(M/s)");
rateLabel.setLocation(50, 50);
rateLabel.setSize(200, 50);
rateLabel.setBackground(Color.YELLOW);
f.add(rateLabel);

ipListLabel = new Label("Receiver IP List");
ipListLabel.setLocation(300, 50);
ipListLabel.setSize(300,50);
ipListLabel.setBackground(Color.YELLOW);
f.add(ipListLabel);


btStart = new Button("Start");
btStart.setLocation(650, 50);
btStart.setSize(100, 50);
btStart.setBackground(Color.GREEN);
f.add(btStart);
btEnd = new Button("End");
btEnd.setLocation(800, 50);
btEnd.setSize(100, 50);
btEnd.setBackground(Color.RED);
f.add(btEnd);

rateText = new TextField();
rateText.setLocation(50, 120);
rateText.setSize(200, 50);
rateText.setBackground(Color.CYAN);
f.add(rateText);

ipText = new TextArea();
ipText.setLocation(300, 120);
ipText.setSize(300, 300);
ipText.setBackground(Color.CYAN);
f.add(ipText);

debugText = new TextArea();
debugText.setLocation(650, 120);
debugText.setSize(200, 300);
debugText.setBackground(Color.CYAN);
f.add(debugText);



f.pack();
f.setVisible(true);
}

public static void addEvents(){

f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});




//为按钮添加事件监听器.
btStart.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getSource() == btStart){
udpStart();
}
}
});
btEnd.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getSource() == btEnd){
udpEnd();
}
}
});
// 添加选项卡选中状态改变的监听器
jtp.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
System.out.println("当前选中的选项卡: " + jtp.getSelectedIndex());
}
});
}
protected static void udpStart() {
// TODO Auto-generated method stub
//获取参数
rate = Double.valueOf(rateText.getText());
String strs = ipText.getText();
String[] split = strs.split("\r\n");
receiveIp.clear();
for(int i=0;i<split.length;i++)
receiveIp.add(split[i]);
int i=0;
while(true)
{
rateText.setText("start.i="+i);
i++;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

protected static void udpEnd() {
// TODO Auto-generated method stub



}

public static void main(String[] args)
{
init();

addEvents();
}
}
...全文
86 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_39936465 2019-01-17
  • 打赏
  • 举报
回复
自己加入 jtp.setLocation(int x,int y);选卡位置 jtp.setSize(int width,int heigth);选卡大小
qq_39936465 2019-01-17
  • 打赏
  • 举报
回复
既然你f.setLayout(null); 用了绝对布局了,你的选卡怎么不设置位置大小呢。 而且和你开始f.setLayout(new FlowLayout());矛盾。设置布局开始设一次就可以了。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧