我正在尝试向JScrollPane
添加JTextArea
,但是当我添加JPane
时,textarea
不再显示我的JScrollPane
。没有textArea
它会显示它,但是我无法显示从文件中检索到的所有信息。
这是我想要用JScrollPane
包裹的 public GUI_CWK()
{
//frame details
setSize(450,400);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("H.o. | Location Database");
setExtendedState(JFrame.MAXIMIZED_BOTH);
getContentPane().setBackground(Color.YELLOW);
// Useing Lable to set a title for multiple buttons
lbl2 = new JLabel();
lbl2.setSize(420,85);
lbl2.setLocation(15,0);
lbl2.setEnabled(false);
lbl2.setFont(new Font ("arial",5,17));
lbl2.setText("Options to Manipulate with Data");
add(lbl2);
// Adding buttons to JFrame
btn1 = new JButton("Add new entry");
btn1.setSize(120,20);
btn1.setLocation(7,80);
btn1.addActionListener(this);
add(btn1);
btn2 = new JButton("Search");
btn2.setSize(120,20);
btn2.setLocation(7,110);
btn2.addActionListener(this);
add(btn2);
btn3 = new JButton("Update Entry");
btn3.setSize(120,20);
btn3.setLocation(150,110);
btn3.addActionListener(this);
add(btn3);
btn4 = new JButton("Print All");
btn4.setSize(120,20);
btn4.setLocation(150,80);
btn4.addActionListener(this);
add(btn4);
btn5 = new JButton("Print Arrays");
btn5.setSize(120,20);
btn5.setLocation(7,140);
btn5.addActionListener(this);
add(btn5);
btn6 = new JButton("Delete Entry");
btn6.setSize(120,20);
btn6.setLocation(300,80);
btn6.addActionListener(this);
add(btn6);
btn7 = new JButton("Delete ALL");
btn7.setSize(120,20);
btn7.setLocation(300,110);
btn7.addActionListener(this);
add(btn7);
lbl1 = new JLabel();
lbl1.setSize(420,85);
lbl1.setLocation(15,190);
lbl1.setEnabled(false);
lbl1.setFont(new Font ("arial",5,17));
lbl1.setText("Extra Options");
add(lbl1);
btn8 = new JButton("Sort Data");
btn8.setSize(160,20);
btn8.setLocation(7,265);
btn8.addActionListener(this);
add(btn8);
btn9 = new JButton("Open Text file");
btn9.setSize(160,20);
btn9.setLocation(7,300);
btn9.addActionListener(this);
add(btn9);
btn11 = new JButton("Retrieve from text file");
btn11.setSize(160,20);
btn11.setLocation(240,265);
btn11.addActionListener(this);
add(btn11);
btn12 = new JButton("Exit Program");
btn12.setSize(160,20);
btn12.setLocation(240,300);
btn12.addActionListener(this);
add(btn12);
lbl3 = new JLabel();
lbl3.setSize(420,85);
lbl3.setLocation(15,340);
lbl3.setEnabled(false);
lbl3.setFont(new Font ("arial",5,17));
lbl3.setText("Terminal Window");
add(lbl3);
// Adding textArea to display each user entered value
textArea2 = new JTextArea(5, 20);
textArea2.setEditable(false);
textArea2.setSize(440,300);
textArea2.setLocation(7,400);
textArea2.setDisabledTextColor(Color.black);
textArea2.setFont(new Font("arial",5,14));
sp2 = new JScrollPane(textArea2);
sp2.setVerticalScrollBarPolicy ( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS );
add(sp2);
lbl4 = new JLabel();
lbl4.setSize(420,40);
lbl4.setLocation(570,10);
lbl4.setEnabled(false);
lbl4.setFont(new Font ("arial",5,17));
lbl4.setText("Data Display");
add(lbl4);
textArea = new JTextArea(55, 50);
textArea.setEditable(false);
textArea.setSize(800,640);
textArea.setLocation(560,60);
textArea.setFont(new Font("arial",5,19));
textArea.setDisabledTextColor(Color.black);
sp = new JScrollPane(textArea);
sp.setVerticalScrollBarPolicy ( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS );
add(sp);
setVisible(true);
}
之一的代码。
(sender as BackgroundWorker).ReportProgress(i/(float)totalFiles,msgString)
答案 0 :(得分:0)
我认为这可以解决您的问题。
您会注意到我删除了this
方法之间的addActionListener()
,因为您必须指定ActionListener
要做的事情。
StartingPoint
(主要)课程:
public class StartingPoint{
public static void main(String[] args){
GUI_CWK gui_cwk = new GUI_CWK(); //Creates a new instance of "GUI_CWK".
gui_cwk.setVisible(true); //You set the gui_cwk to visible.
}
}
GUI_CWK
上课:
import java.awt.Color;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
public class GUI_CWK extends JFrame{
private static final long serialVersionUID = 1L;
public GUI_CWK() {
// frame details
this.setSize(450, 400);
JPanel panel = new JPanel();
this.setLayout(null); /* Each time that you want to refer to the classyou are in, you say "this" */
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("H.o. | Location Database");
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
this.getContentPane().setBackground(Color.YELLOW);
// Using labels to set a title for multiple buttons
JLabel lbl2 = new JLabel(); /*Remember that you have to create the instances of an object by naming the class before the name of the instance */
lbl2.setSize(420, 85);
lbl2.setLocation(15, 0);
lbl2.setEnabled(false);
lbl2.setFont(new Font("arial", 5, 17));
lbl2.setText("Options to Manipulate with Data");
panel.add(lbl2);
// Adding buttons to JFrame
JButton btn1 = new JButton("Add new entry");
btn1.setSize(120, 20);
btn1.setLocation(7, 80);
btn1.addActionListener(/* Insert ActionListener */);
panel.add(btn1); //The button is added to the panel, so then the last one can be added to the JFrame at the end.
JButton btn2 = new JButton("Search");
btn2.setSize(120, 20);
btn2.setLocation(7, 110);
btn2.addActionListener(/* Insert ActionListener */);
panel.add(btn2);
JButton btn3 = new JButton("Update Entry");
btn3.setSize(120, 20);
btn3.setLocation(150, 110);
btn3.addActionListener(/* Insert ActionListener */);
panel.add(btn3);
JButton btn4 = new JButton("Print All");
btn4.setSize(120, 20);
btn4.setLocation(150, 80);
btn4.addActionListener(/* Insert ActionListener */);
panel.add(btn4);
JButton btn5 = new JButton("Print Arrays");
btn5.setSize(120, 20);
btn5.setLocation(7, 140);
btn5.addActionListener(/* Insert ActionListener */);
panel.add(btn5);
JButton btn6 = new JButton("Delete Entry");
btn6.setSize(120, 20);
btn6.setLocation(300, 80);
btn6.addActionListener(/* Insert ActionListener */);
panel.add(btn6);
JButton btn7 = new JButton("Delete ALL");
btn7.setSize(120, 20);
btn7.setLocation(300, 110);
btn7.addActionListener(/* Insert ActionListener */);
panel.add(btn7);
JLabel lbl1 = new JLabel();
lbl1.setSize(420, 85);
lbl1.setLocation(15, 190);
lbl1.setEnabled(false);
lbl1.setFont(new Font("arial", 5, 17));
lbl1.setText("Extra Options");
panel.add(lbl1);
JButton btn8 = new JButton("Sort Data");
btn8.setSize(160, 20);
btn8.setLocation(7, 265);
btn8.addActionListener(/* Insert ActionListener */);
panel.add(btn8);
JButton btn9 = new JButton("Open Text file");
btn9.setSize(160, 20);
btn9.setLocation(7, 300);
btn9.addActionListener(/* Insert ActionListener */);
panel.add(btn9);
JButton btn11 = new JButton("Retrieve from text file");
btn11.setSize(160, 20);
btn11.setLocation(240, 265);
btn11.addActionListener(/* Insert ActionListener */);
panel.add(btn11);
JButton btn12 = new JButton("Exit Program");
btn12.setSize(160, 20);
btn12.setLocation(240, 300);
btn12.addActionListener(/* Insert ActionListener */);
panel.add(btn12);
JLabel lbl3 = new JLabel();
lbl3.setSize(420, 85);
lbl3.setLocation(15, 340);
lbl3.setEnabled(false);
lbl3.setFont(new Font("arial", 5, 17));
lbl3.setText("Terminal Window");
panel.add(lbl3);
// Adding textArea to display each user entered value
JTextArea textArea2 = new JTextArea(5, 20);
textArea2.setEditable(false);
textArea2.setSize(440, 300);
textArea2.setLocation(7, 400);
textArea2.setDisabledTextColor(Color.black);
textArea2.setFont(new Font("arial", 5, 14));
JScrollPane sp2 = new JScrollPane(textArea2);
sp2.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
textArea2.add(sp2); //Here you add the scrollPanel to the textArea,
//in the original code you were adding it to the frame
panel.add(textArea2);
JLabel lbl4 = new JLabel();
lbl4.setSize(420, 40);
lbl4.setLocation(570, 10);
lbl4.setEnabled(false);
lbl4.setFont(new Font("arial", 5, 17));
lbl4.setText("Data Display");
panel.add(lbl4);
JTextArea textArea = new JTextArea(55, 50);
textArea.setEditable(false);
textArea.setSize(800, 640);
textArea.setLocation(560, 60);
textArea.setFont(new Font("arial", 5, 19));
textArea.setDisabledTextColor(Color.black);
JScrollPane sp1 = new JScrollPane(textArea);
sp1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
textArea.setVisible(true);
textArea.add(sp1);
panel.add(textArea);
this.add(panel); //Everything which was added to the panel now is added to the frame.
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
答案 1 :(得分:0)
以下是使用滚动窗格的示例。
import javax.swing.*;
import java.awt.*;
public class ScrollPaneExample{
public void buildGui(){
JFrame frame = new JFrame("button and scrollpane");
JButton button = new JButton("example");
JTextArea area = new JTextArea(20, 20);
//this is where we will place our components.
//It has a flow layout by default.
JPanel content = new JPanel();
JScrollPane sp = new JScrollPane(area);
content.add(button);
content.add(sp);
//this line limits the size of the scroll pane.
sp.setPreferredSize(new Dimension(400, 60));
//The frame will use our panel instead of the default one.
frame.setContentPane(content);
//sizes the frame to the content area.
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args){
EventQueue.invokeLater(()->{new ScrollPaneExample().buildGui();});
}
}
主要区别在于我设置了内容窗格,因此我知道它的布局管理器。您似乎手动设置了所有尺寸,我很惊讶它的工作原理。你应该选择一个好的布局管理器。 pick one你认为你做了什么。
另请注意,这是一个将编译的最小示例。
答案 2 :(得分:0)
谢谢你的帮助,我找到了答案。正如@matt告诉我的那样,我应该为JScrollPane而不是textArea添加大小和位置。所以我做到了,现在对我有用。
代码如下。
textArea2 = new JTextArea(5,20);
textArea2.setEditable(false);
textArea2.setDisabledTextColor(Color.black);
textArea2.setFont(new Font("arial",5,14));
sp2 = new JScrollPane(textArea2);
sp2.setSize(440,300);
sp2.setLocation(7,400);
sp2.setVerticalScrollBarPolicy ( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS );
add(sp2);