两个共有两个java文件
mainFrame.java 主文件 和btn.java 文件本程序主要功能在mainFrame 输出1024 个圆形按钮 ,在给按钮加上tooltip 提示文件...现在的问题是 输出的前4个按钮的tooltip 提示都为0 第5个按钮才为1不知道是怎么回事.最后一个按钮为1020 我不明白...代码如下
import java.awt.*;import javax.comm.CommPortIdentifier;
import javax.comm.SerialPort;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;
import javax.swing.*;import java.awt.event.*;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import javax.comm.*;
public class mainFrame extends JFrame implements SerialPortEventListener{
private JLabel l1 = new JLabel("Com:");
private JComboBox co1 = new JComboBox();
private JButton b1 = new JButton("Start");
Container con = null;
private btn bc = new btn(this);
SerialPort serialPort;
CommPortIdentifier portId;
OutputStream outputStream = null;
InputStream inputStream = null;
Enumeration portList=null;
String portName = null;
private JLabel lm[] = new JLabel[1024];
private btn bnn[]= new btn[1024];
private JButton exit = new JButton("exit");
public mainFrame(){
con =this.getContentPane();
con.setLayout(null);
b1.setBounds(180, 15, 70, 20);
l1.setBounds(15,15,70,20);
co1.setBounds(50,15,70,20);
bc.setBounds(140,15,20,20);
exit.setBounds(270, 15, 60, 20);
con.add(exit);
con.add(bc);
con.add(l1);
con.add(co1);
con.add(b1);
this.co1.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e) {
co1_button(e);
}
});
exit.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { exit_button(e);
}
});
this.getPort();
this.loading_button();
this.setTitle("AD 接收段");
int width = this.getToolkit().getScreenSize().width;
int height = this.getToolkit().getScreenSize().height-30;
this.setVisible(true);
this.setSize(width,height);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void loading_button(){
int x = 20;
int y = 50;
int bb = 0;
for(int row=0;row<16;row++){
for(int col=0;col<64;col++ ){
bnn[bb] = new btn(this);
bnn[bb].setBounds(x, y, 60, 20);
bnn[bb].setTip(String.valueOf(bb)); //本程序主要功能在mainFrame 输出1024 个圆形按钮 ,在给按钮加上tooltip 提示文件...现在的问题是 输出的前4个按钮的tooltip 提示都为0 第5个按钮才为1不知道是怎么回事.最后一个按钮为1020 我不明白
con.add(this.bnn[bb]);
x = x+15;
bb++;
}
y = y+35;
x = 20;
}
}
public void exit_button(ActionEvent e){
System.exit(0);
}
public void co1_button(ItemEvent e){
portName = co1.getSelectedItem().toString();
if(e.getStateChange() ==2 && !portName.equals("select")){
this.connectPort();
}
}
public void connectPort(){
if(this.serialPort !=null){
this.serialPort.close();
}
try
{
portId = CommPortIdentifier.getPortIdentifier(this.portName);
serialPort = (SerialPort) portId.open("ReadComm", 2000);
bc.c = new Color(0,128,0);
bc.repaint();
}catch(Exception ee){
JOptionPane.showMessageDialog(this,"当前串口连接失败","Message",JOptionPane.INFORMATION_MESSAGE);
}
try
{
serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
}catch(Exception ee){
JOptionPane.showMessageDialog(this,"Please connect serial port first!","Message",JOptionPane.INFORMATION_MESSAGE);
}
try{
this.inputStream = serialPort.getInputStream();
this.serialPort.addEventListener(this);
}catch(Exception ee){
}
serialPort.notifyOnDataAvailable(true);
}
public void getPort(){
portList = CommPortIdentifier.getPortIdentifiers();
CommPortIdentifier portId=null;
this.co1.addItem("select");
while(portList.hasMoreElements()){
portId = (CommPortIdentifier)portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
this.co1.addItem(portId.getName());
}
}
}
public void serialEvent(SerialPortEvent event) {
switch(event.getEventType()){
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer = new byte[21];
try{
while(inputStream.available()>0){
int numBytes=inputStream.read(readBuffer);
}
JOptionPane.showMessageDialog(this, new String(readBuffer),"当前接收字符",JOptionPane.ERROR_MESSAGE);
}catch(Exception ee){
}
break;
}
}
public static void main(String args[]){
new mainFrame();
}}btn.javaimport javax.swing.*;import java.awt.*;
import java.awt.event.*;public class btn extends JButton{
public Color c = new Color(128,128,128);
mainFrame m = null;
public btn(mainFrame mm){
this.m =mm;
}
public void setTip(String str){
this.setToolTipText(str);
System.out.println("传进来的是:"+str);
}
public void paint(Graphics g){
g.setColor(c);
g.fillOval(1, 1, 10, 10);
}}
mainFrame.java 主文件 和btn.java 文件本程序主要功能在mainFrame 输出1024 个圆形按钮 ,在给按钮加上tooltip 提示文件...现在的问题是 输出的前4个按钮的tooltip 提示都为0 第5个按钮才为1不知道是怎么回事.最后一个按钮为1020 我不明白...代码如下
import java.awt.*;import javax.comm.CommPortIdentifier;
import javax.comm.SerialPort;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;
import javax.swing.*;import java.awt.event.*;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import javax.comm.*;
public class mainFrame extends JFrame implements SerialPortEventListener{
private JLabel l1 = new JLabel("Com:");
private JComboBox co1 = new JComboBox();
private JButton b1 = new JButton("Start");
Container con = null;
private btn bc = new btn(this);
SerialPort serialPort;
CommPortIdentifier portId;
OutputStream outputStream = null;
InputStream inputStream = null;
Enumeration portList=null;
String portName = null;
private JLabel lm[] = new JLabel[1024];
private btn bnn[]= new btn[1024];
private JButton exit = new JButton("exit");
public mainFrame(){
con =this.getContentPane();
con.setLayout(null);
b1.setBounds(180, 15, 70, 20);
l1.setBounds(15,15,70,20);
co1.setBounds(50,15,70,20);
bc.setBounds(140,15,20,20);
exit.setBounds(270, 15, 60, 20);
con.add(exit);
con.add(bc);
con.add(l1);
con.add(co1);
con.add(b1);
this.co1.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e) {
co1_button(e);
}
});
exit.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { exit_button(e);
}
});
this.getPort();
this.loading_button();
this.setTitle("AD 接收段");
int width = this.getToolkit().getScreenSize().width;
int height = this.getToolkit().getScreenSize().height-30;
this.setVisible(true);
this.setSize(width,height);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void loading_button(){
int x = 20;
int y = 50;
int bb = 0;
for(int row=0;row<16;row++){
for(int col=0;col<64;col++ ){
bnn[bb] = new btn(this);
bnn[bb].setBounds(x, y, 60, 20);
bnn[bb].setTip(String.valueOf(bb)); //本程序主要功能在mainFrame 输出1024 个圆形按钮 ,在给按钮加上tooltip 提示文件...现在的问题是 输出的前4个按钮的tooltip 提示都为0 第5个按钮才为1不知道是怎么回事.最后一个按钮为1020 我不明白
con.add(this.bnn[bb]);
x = x+15;
bb++;
}
y = y+35;
x = 20;
}
}
public void exit_button(ActionEvent e){
System.exit(0);
}
public void co1_button(ItemEvent e){
portName = co1.getSelectedItem().toString();
if(e.getStateChange() ==2 && !portName.equals("select")){
this.connectPort();
}
}
public void connectPort(){
if(this.serialPort !=null){
this.serialPort.close();
}
try
{
portId = CommPortIdentifier.getPortIdentifier(this.portName);
serialPort = (SerialPort) portId.open("ReadComm", 2000);
bc.c = new Color(0,128,0);
bc.repaint();
}catch(Exception ee){
JOptionPane.showMessageDialog(this,"当前串口连接失败","Message",JOptionPane.INFORMATION_MESSAGE);
}
try
{
serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
}catch(Exception ee){
JOptionPane.showMessageDialog(this,"Please connect serial port first!","Message",JOptionPane.INFORMATION_MESSAGE);
}
try{
this.inputStream = serialPort.getInputStream();
this.serialPort.addEventListener(this);
}catch(Exception ee){
}
serialPort.notifyOnDataAvailable(true);
}
public void getPort(){
portList = CommPortIdentifier.getPortIdentifiers();
CommPortIdentifier portId=null;
this.co1.addItem("select");
while(portList.hasMoreElements()){
portId = (CommPortIdentifier)portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
this.co1.addItem(portId.getName());
}
}
}
public void serialEvent(SerialPortEvent event) {
switch(event.getEventType()){
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer = new byte[21];
try{
while(inputStream.available()>0){
int numBytes=inputStream.read(readBuffer);
}
JOptionPane.showMessageDialog(this, new String(readBuffer),"当前接收字符",JOptionPane.ERROR_MESSAGE);
}catch(Exception ee){
}
break;
}
}
public static void main(String args[]){
new mainFrame();
}}btn.javaimport javax.swing.*;import java.awt.*;
import java.awt.event.*;public class btn extends JButton{
public Color c = new Color(128,128,128);
mainFrame m = null;
public btn(mainFrame mm){
this.m =mm;
}
public void setTip(String str){
this.setToolTipText(str);
System.out.println("传进来的是:"+str);
}
public void paint(Graphics g){
g.setColor(c);
g.fillOval(1, 1, 10, 10);
}}
public void loading_button()方法中的bnn[bb].setBounds(x, y, 60, 20);
60这个数值造成的.你改为:bnn[bb].setBounds(x, y, 20, 20); 就行了.