JScrollpane
上无法看到JTextArea
...
有人能找出我的代码中有什么问题吗?
以及如何解决它?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.text.DateFormat;
class PrimaryInterface extends JFrame implements ActionListener
{
ImageIcon iimages;
JLabel jlimageContainer,jltime,jltitle,jlname1,jlname2;
JLabel jlnoValue;
JLabel jlnumberOfThreads, jlnameOfthreads,jlstatusOfThreads, jllane1;
JButton jbtstartSlideShow,jbtstopSlideShow;
Font ftimeFont,ftitleFont,fnameFont;
JProgressBar progbarMain,progbarLock;
JProgressBar progbarThread[] = new JProgressBar[10];
JButton buttonGo;
JButton buttonStart;
JButton buttonStop;
JPanel panelMain,panelLocker,progPanel,sample;
JTextArea textArea;
JScrollPane scrPane;
JRadioButton radioButton[] = new JRadioButton[10];
ButtonGroup buttonGroup;
int threadCount=1,targetThread=0;
firstTester ft[] = new firstTester[10];
secondTester st[] = new secondTester[10];
firstLocker first[] = new firstLocker[10];
PrimaryInterface()
{
super("SLIDE SHOW");
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paintImageContainer()
{
jlname1= new JLabel("THREAD 1");
jlname2=new JLabel("THREAD 2");
jltitle = new JLabel("DEADLOCK APPLICATION",JLabel.CENTER);
jltitle.setBounds(350,5,300,50);
jlimageContainer = new JLabel();
jltime = new JLabel();
jbtstartSlideShow = new JButton("StartShow");
jbtstopSlideShow = new JButton("StopShow");
jlnoValue = new JLabel();
ftimeFont = new Font("Arial",1,40);
fnameFont = new Font("Arial",1,16);
ftitleFont = new Font("Constantia",1,25);
jbtstartSlideShow.setBounds(40,300,100,25);
jbtstopSlideShow.setBounds(220,300,100,25);
jlimageContainer.setBounds(80,80,200,200);
jlimageContainer.setBorder(BorderFactory.createEtchedBorder(Color.BLACK,Color.WHITE));
jlimageContainer.setText("SLIDESHOW AREA");
jltime.setBounds(500,160,300,100);
jltime.setForeground(Color.blue);
jlname1.setFont(fnameFont);
jlname2.setFont(fnameFont);
jltime.setFont(ftimeFont);
jltitle.setFont(ftitleFont);
jlname1.setBounds(10,30,100,30);
jlname2.setBounds(500,130,100,30);
jltitle.setForeground(Color.red);
jlimageContainer.setText("SLIDESHOW AREA");
textArea = new JTextArea(180, 30);
scrPane=new JScrollPane(textArea);
new DigitalWatch(jltime,jlnoValue);
new SlideShow(iimages,jlimageContainer,jbtstartSlideShow,jbtstopSlideShow,jlnoValue);
jlnumberOfThreads = new JLabel("Number of Threads:");
jlnameOfthreads = new JLabel();
jlstatusOfThreads = new JLabel("Thread Status:");
jllane1 = new JLabel();
jllane1.setBorder(BorderFactory.createEtchedBorder(Color.BLACK,Color.WHITE));
jllane1.setBounds(20,340,830,4);
jlnameOfthreads.setBorder(BorderFactory.createEtchedBorder(Color.BLACK,Color.WHITE));
jlnameOfthreads.setBounds(15,560,830,140);
jlnumberOfThreads.setBounds(20,560,200,20);
jlnoValue.setBounds(250,560,200,20);
jlstatusOfThreads.setBounds(20,580,200,20);
panelMain = new JPanel(new GridLayout(1,1));
panelLocker = new JPanel(new GridLayout(1,1));
progPanel = new JPanel(new GridLayout(10,2));
//panelMain.setLayout(null);
//panelLocker.setLayout(null);
panelMain.setBounds(500,260,180,30);
panelLocker.setBounds(20,400,180,30);
progPanel.setBounds(250,350,300,200);
buttonStart = new JButton("START");
buttonStop = new JButton("DEADLOCK");
buttonGroup = new ButtonGroup();
progbarMain = new JProgressBar();
//panelMain.setLayout(null);
//panelLocker.setLayout(null);
buttonStart.setBounds(600,400,100,30);
buttonStop.setBounds(600,450,100,30);
textArea.setBounds(50,600,750,80);
textArea.setLineWrap(true);
panelMain.add(progbarMain);
progbarMain.setIndeterminate(true);
progbarMain.setStringPainted(true);
progbarMain.setString("Main Thread");
progbarLock = new JProgressBar();
progbarLock.setIndeterminate(true);
progbarLock.setStringPainted(true);
progbarLock.setString("DeadLock Thread");
panelLocker.add(progbarLock);
scrPane.setAutoscrolls(true);
buttonStop.setEnabled(false);
//setContentPane(panelMain);
//new PrimaryInterface();
add(buttonStart);
add(buttonStop);
add(textArea);
buttonStart.addActionListener(this);
buttonStop.addActionListener(this);
add(jlname1);
add(jlname2);
add(jltitle);
add(jlimageContainer);
add(jbtstartSlideShow);
add(jbtstopSlideShow);
add(jltime);
add(jlnumberOfThreads);
add(jlnameOfthreads);
add(jllane1);
add(jlstatusOfThreads);
add(jlnoValue);
add(progPanel);
add(panelMain);
add(panelLocker);
setSize(900,730);
setVisible(true);
this.getContentPane().setBackground(Color.yellow.darker());
}
public void createProgress(int tNum)
{
radioButton[tNum] = new JRadioButton("Thread "+String.valueOf(tNum));
buttonGroup.add(radioButton[tNum]);
progbarThread[tNum] = new JProgressBar();
if(tNum!=0)
{
progPanel.add(radioButton[tNum]);
progPanel.add(progbarThread[tNum]);
}
radioButton[tNum].addActionListener(this);
buttonStop.setEnabled(false);
progbarThread[tNum].setIndeterminate(true);
progbarThread[tNum].setStringPainted(true);
progbarThread[tNum].setString("Thread "+tNum);
}
public void actionPerformed(ActionEvent ae)
{
String command = ae.getActionCommand();
if(command.equals("START"))
{
textArea.append("Creating a new thread : Thread "+threadCount+"\n");
ft[threadCount] = new firstTester();
st[threadCount] = new secondTester();
first[threadCount] = new firstLocker(String.valueOf(threadCount),ft[threadCount],st[threadCount],false,this);
threadCount++;
}
if(command.equals("DEADLOCK"))
{
textArea.append("Creating Deadlock thread : Thread 0\n");
progbarThread[targetThread].setIndeterminate(false);
progbarThread[targetThread].setValue(0);
progbarLock.setIndeterminate(false);
progbarLock.setValue(0);
first[targetThread] = new firstLocker("0",ft[targetThread],st[targetThread],true,this);
secondLocker second = new secondLocker("0",ft[targetThread],st[targetThread],this);
radioButton[targetThread].setEnabled(false);
progbarThread[targetThread].setString("Thread "+targetThread+" in deadlock");
}
for(int actionCounter = 0;actionCounter<=10;actionCounter++)
{
if(command.equals("Thread "+actionCounter))
{
targetThread = actionCounter;
textArea.append("Thread "+actionCounter+" selected.\n");
buttonStop.setEnabled(true);
progbarLock.setIndeterminate(true);
progbarLock.setString("DeadLock Thread");
}
}
}
}
class SlideShow implements Runnable,ActionListener
{
ImageIcon iimages;
JLabel jlimageContainer,jlnoValue;
JButton jbtstartSlideShow,jbtstopSlideShow;
Thread t1;
String path="h:\\images";
int i = 0;
int j = 0;
SlideShow(ImageIcon images,JLabel imageContainer,JButton startSlideShow,JButton stopSlideShow, JLabel noValue)
{
iimages = images;
jlimageContainer = imageContainer;
jlnoValue = noValue;
jbtstartSlideShow = startSlideShow;
jbtstopSlideShow = stopSlideShow;
jbtstartSlideShow.addActionListener(this);
jbtstopSlideShow.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource() == jbtstartSlideShow)
{
t1 = new Thread(this);
t1.start();
jbtstartSlideShow.setEnabled(false);
}
if(ae.getSource() == jbtstopSlideShow)
{
t1.stop();
jbtstartSlideShow.setEnabled(true);
}
}
public void startSlideShow()
{
try
{
for(i=1;i<=5;i++)
{
iimages = new ImageIcon(path+"\\"+i+".jpg");
jlimageContainer.setIcon(iimages);
t1.sleep(2000);
}
}
catch(Exception e)
{
}
}
public void run()
{
jlnoValue.setText((1+(Thread.currentThread().activeCount()-4)/2.0)+"");
jlimageContainer.setText("LOADING...");
for(j=1;j<=2;j++)
{
startSlideShow();
if(j==2)
j=0;
}
}
}
class DigitalWatch implements Runnable
{
static Thread t2;
JLabel labelTime,jlnoValue;
String sformattedTime="";
DigitalWatch(JLabel objL,JLabel noValue)
{
labelTime = objL;
jlnoValue = noValue;
t2 = new Thread(this);
}
public static void showWatch()
{
t2.start();
}
public void run()
{
jlnoValue.setText("1");
for(;;)
{
Date rawTime=new Date();
sformattedTime = DateFormat.getTimeInstance(DateFormat.MEDIUM).format(rawTime);
labelTime.setText(sformattedTime);
System.out.println(sformattedTime);
try{
t2.sleep(1000);
}
catch(Exception e)
{
}
}
}
}
//Synchronised Test method 1
class firstTester{
static int fCount;
boolean flagLock;
firstTester()
{
System.out.println("Test Class 1 started");
}
synchronized void test1(int tNum,secondTester second,boolean flagLocker,PrimaryInterface pInterface)
{
//pInterface.textArea.append("Thread :"+tNum+" count :"+fCount+"\n");
flagLock = flagLocker;
try{
Thread.sleep(150);
}catch(Exception e){}
fCount++;
if(flagLock)
{
pInterface.textArea.append("Creating Dead lock....");
second.test2(tNum,this,flagLock,pInterface);
}
}
}
//Synchronised Test method 2
class secondTester{
boolean flagLock;
secondTester()
{
System.out.println("Test Class 2 started");
}
synchronized void test2(int tNum,firstTester first,boolean flagLocker,PrimaryInterface pInterface)
{
flagLock = flagLocker;
try{
Thread.sleep(100);
}catch(Exception e){}
if(flagLock)
{
first.test1(tNum,this,flagLock,pInterface);
}
}
}
class firstLocker implements Runnable{
Thread t;
String tName;
int tNum;
firstTester first;
secondTester second;
boolean flagReady = false;
PrimaryInterface pInterface;
firstLocker(String name,firstTester first,secondTester second,boolean flagLock,PrimaryInterface pInterface)
{
flagReady = flagLock;
System.out.println("Starting Thread"+name);
this.first = first;
this.second = second;
this.pInterface = pInterface;
tName = name;
tNum = Integer.parseInt(tName);
pInterface.createProgress(tNum);
t = new Thread(this);
t.start();
}
public void run()
{
while(true)
{
first.test1(tNum,second,flagReady,pInterface);
second.test2(tNum,first,flagReady,pInterface);
}
}
}
class secondLocker implements Runnable{
Thread t;
String tName;
int tNum;
firstTester first;
secondTester second;
PrimaryInterface pInterface;
secondLocker(String name,firstTester first,secondTester second,PrimaryInterface pInterface)
{
System.out.println("Starting Thread"+name);
this.first = first;
this.second = second;
this.pInterface = pInterface;
tName = name;
tNum = Integer.parseInt(tName);
t = new Thread(this);
t.start();
}
public void run()
{
second.test2(tNum,first,true,pInterface);
}
}
class Caution
{
public static void main(String args[])
{
PrimaryInterface pInterface = new PrimaryInterface();
pInterface.paintImageContainer();
DigitalWatch.showWatch();
}
}
答案 0 :(得分:3)
正确地实例化JTextArea和JScrollPane,并将它们正确绑定在一起,然后将JTextArea直接添加到PrimaryInterface。请尝试添加JScrollPane。
简而言之,它应该是这样的:
JTextArea textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView( textArea );
add( scrollPane );
我注意到你没有使用LayoutManager。也许您应该使用LayoutManager而不是自己设置所有边界。这将减少不可预测行为的实例,并使您的UI在调整帧大小等条件下正常运行。
答案 1 :(得分:2)
太长了;没读! :-)
您似乎将textarea添加到父容器中。您应该添加滚动窗格:
你有:
add(textArea);
尝试
add(new JScrollPane(textArea));
解决这类问题的建议:
创建可用于演示问题的最小测试类。类似的东西:
public class TestFrame {
public static void main(String args[]) {
JFrame frame = new JFrame("Test");
// add some stuff to the frame
frame.add(...);
frame.pack();
frame.setVisible(true);
}
}
通常在创建这样一个最小的测试类时,我偶然发现了解决方案。但无论如何,我们很难在小代码中发现问题。
答案 2 :(得分:1)
正如已经指出的,一旦将textArea添加到scrPane,就应该将scrPane添加到容器中。你不应该忘记布局scrPane。
您需要更改两件事:
textArea.setBounds(50,600,750,80);
应该是:
scrPane.setBounds(50,600,750,80);
和
add(textArea);
应该是:
add(scrPane);