import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
public class add
{
JFrame frame = new JFrame("Add");
JPanel ButtonPanel = new JPanel();
JButton addButton1 = new JButton("Add Presenter");
JButton addButton2 = new JButton("Add Attendee");
JTextField textField1 = new JTextField();
JTextField textField2 = new JTextField();
DrawPanel drawpanel = new DrawPanel();
JScrollPane scrollPane = new JScrollPane(drawpanel,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
public static void main(String args[])
{
add a = new add();
a.hh();
}
public void hh()
{
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.eait(0);
}});
addButton1.addActionListener(new ActionListener() //为addButton1添加监听器
{
public void actionPerformed(ActionEvent ae)
{
String text1 = textField1.getText();
drawpanel.addElementP(text1);
drawpanel.changeSizeP();
drawpanel.repaint();
}
});
addButton2.addActionListener(new ActionListener() //为addButton2添加监听器
{
public void actionPerformed(ActionEvent ae)
{
String text2 = textField2.getText();
drawpanel.addElementA(text2);
drawpanel.changeSizeA();
drawpanel.changeSizeP();
drawpanel.repaint();
}
});
ButtonPanel.setLayout(new GridLayout(2,2));
ButtonPanel.add(addButton1);
ButtonPanel.add(addButton2);
ButtonPanel.add(textField1);
ButtonPanel.add(textField2);
frame.getContentPane().add(scrollPane,BorderLayout.CENTER);
frame.getContentPane().add(ButtonPanel,BorderLayout.SOUTH);
frame.setSize(new Dimension(250,293));
frame.setVisible(true);
}
class DrawPanel extends JPanel
{
final Color fg = Color.black;
Vector vect1 = new Vector();
Vector vect2 = new Vector();
int p= vect1.size();
int q= vect2.size();
int i;
int j;
public void changeSizeP()
{
p = vect1.size();
}
public void changeSizeA()
{
q = vect2.size();
}
public void paintComponent(Graphics g) //画面板的结构
{
super.paintComponent(g);
Color fg3D = Color.lightGray;
g.setColor(fg3D);
g.draw3DRect(0, 0, 50, 30,true);
g.draw3DRect(50,0,150,30,true);
g.draw3DRect(200,0,50,30,true);
g.fillRect(50,30,150,30);
g.setColor(Color.black);
g.drawLine(50,60,200,60);
g.drawLine(0,30,250,30);
g.drawLine(200,30,200,240);
g.drawLine(50,90,200,90);
g.drawLine(50,120,200,120);
g.drawLine(50,150,200,150);
g.drawLine(50,180,200,180);
g.drawLine(50,210,200,210);
g.setColor(Color.white);
g.setColor(Color.blue);
if (p == 0){}
else{//在Presenter列不为空时显示所有元素名
for (i = 0;i<p;i++)
{
Object str3 = vect1.elementAt(i);
g.drawString((String)str3,50+5,(i+2)*30+20);
g.setColor(Color.black);
g.drawLine(50,(i+3)*30,200,(i+3)*30);
g.drawLine(50,(i+2)*30,50,(i+3)*30);
g.drawLine(200,(i+2)*30,200,(i+3)*30);
}
}
if (q == 0){}
else{//在Attendee列不为空时显示所有元素名
for (j = 0;j<q;j++)
{
Object str4 = vect2.elementAt(j);
g.drawString((String)str4,50+5,(p+j+3)*30+20);
g.setColor(Color.black);
g.drawLine(50,(j+p+4)*30,200,(j+p+4)*30);
g.drawLine(50,(j+p+3)*30,50,(j+p+4)*30);
g.drawLine(200,(j+p+3)*30,200,(j+p+4)*30);
}
}
g.drawString("Presenter:"+p,50+5,30+20);
g.drawString("Partici:"+(p+q),50+5,0+20);
g.setColor(fg3D);
g.fillRect(50,(p+2)*30,150,30);
g.setColor(Color.black);
g.drawLine(50,(p+2)*30,200,(p+2)*30);
g.drawLine(50,(p+3)*30,200,(p+3)*30);
g.drawLine(50,(p+2)*30,50,(p+3)*30);
g.drawLine(200,(p+2)*30,200,(p+3)*30);
g.drawLine(50,30,50,240);
g.setColor(Color.blue);
g.drawString("Attendee:"+q,50+5,(p+2)*30+20);
g.setColor(fg);
}
public void addElementP(String x)//在Presenter中加入新元素
{
if (vect1.size() == 0)
{
vect1.insertElementAt(x,0);
}
else
{
int i=0;
while (i<vect1.size())
{
Object nextOb = vect1.elementAt(i);
if( ! Compare(x,nextOb)) {}
else
{
break;
}
i++;
}
vect1.add(i,x);
}
}
public void addElementA(String y)//在Attendee中加入新元素
{
if (vect2.size() == 0)
{
vect2.insertElementAt(y,0);
}
else
{
int i=0;
while (i<vect2.size())
{
Object nextOb = vect2.elementAt(i);
if( ! Compare(y,nextOb)) {}
else
{
break;
}
i++;
}
vect2.add(i,y);
}
}
public boolean Compare(String m,Object n)//比较两个字符串大小
{
String str1 = m;
Object str2 = n;
if (str1.compareTo((String)str2)>0)
{
return false;
}
else
{
return true;
}
}
}
}
6 个解决方案
#1
panel好像不会因draw而改变大小吧
不如用个JTextArea或其它
不如用个JTextArea或其它
#2
你上次那个问题怎么一点分都不给我
问题就是我所说那样嘛,还说不是关键
问题就是我所说那样嘛,还说不是关键
#3
不好意思,我是第一次给分,我以为一次只能给一个人,而且那位朋友给的代码的确很有用~这次补你的~你有没有运行我的程序,必须用画出来的,不能用别的。怎么样才能让scollbar出来,?
#4
你试试每次添加后就检查行数,根据行数设置panel的大小
#5
up
#6
怎样“根据行数设置panel的大小”?
#1
panel好像不会因draw而改变大小吧
不如用个JTextArea或其它
不如用个JTextArea或其它
#2
你上次那个问题怎么一点分都不给我
问题就是我所说那样嘛,还说不是关键
问题就是我所说那样嘛,还说不是关键
#3
不好意思,我是第一次给分,我以为一次只能给一个人,而且那位朋友给的代码的确很有用~这次补你的~你有没有运行我的程序,必须用画出来的,不能用别的。怎么样才能让scollbar出来,?
#4
你试试每次添加后就检查行数,根据行数设置panel的大小
#5
up
#6
怎样“根据行数设置panel的大小”?