我在这里读了很多其他的问题,所有的建议都一样,但它对我不起作用,没有任何反应。
我的代码是:
final JTextArea ta = new JTextArea();
ta.setPreferredSize(new Dimension(310, 325));
ta.setMinimumSize(new Dimension(310, 325));
ta.setEditable(false);
ta.setFont(new Font("Tahoma", Font.PLAIN, 11));
JScrollPane sp = new JScrollPane(ta);
contentPane.add(sp);
contentPane设置为程序的开始
contentPane = new JPanel();
contentPane.setBackground(Color.GRAY);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
提前致谢:)
编辑:我做了一些调整,现在出现了scrollpane,但是当它出现时,textArea因某种原因消失了..这是在pastebin中的整个程序,只是搜索scrollPane或textArea。谢谢!
答案 0 :(得分:0)
在setPreferedSize()
上使用setMinimumSize()
和JScrollPane
方法。不要在JTextArea
上正确使用它们,因为它不可滚动。所以使用
JScrollPane sp = new JScrollPane(ta);
sp.setPreferredSize(new Dimension(310, 325));
sp.setMinimumSize(new Dimension(310, 325));
而不是
JTextArea ta = new JTextArea();
ta.setPreferredSize(new Dimension(310, 325));
ta.setMinimumSize(new Dimension(310, 325));
并且您的scrollPane将起作用。
答案 1 :(得分:0)
您的代码存在一些问题:
您的整个计划:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;
public class RateTesterWindow extends JFrame {
private JPanel contentPane;
private JTextField textField_1;
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
RateTesterWindow frame = new RateTesterWindow();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public RateTesterWindow() {
setResizable(false);
setTitle("L2PRIDE RATE TESTER v0.1");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 350, 377);
contentPane = new JPanel();
contentPane.setBackground(Color.GRAY);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JLabel lblRateTester = new JLabel("RATE TESTER");
lblRateTester.setFont(new Font("Tahoma", Font.BOLD, 18));
JSeparator separator = new JSeparator();
separator.setBackground(Color.BLACK);
final JLabel lblPleaseEnterRate = new JLabel("Rate:");
lblPleaseEnterRate.setFont(new Font("Tahoma", Font.BOLD, 11));
final JRadioButton rdbtnDreadItem = new JRadioButton("Dread Item");
rdbtnDreadItem.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e)
{
if (rdbtnDreadItem.isSelected())
{
textField.setText("-----");
textField.setEditable(false);
}
else
{
textField.setText("");
textField.setEditable(true);
}
}
});
rdbtnDreadItem.setToolTipText("Will use dread item chances like they are in game.");
JLabel lblScrolls = new JLabel("Scrolls:");
lblScrolls.setFont(new Font("Tahoma", Font.BOLD, 11));
final JLabel lblResults = new JLabel("ABOUT:");
lblResults.setFont(new Font("Tahoma", Font.BOLD, 12));
JSeparator separator_1 = new JSeparator();
JSeparator separator_2 = new JSeparator();
separator_2.setBackground(Color.BLACK);
JSeparator separator_3 = new JSeparator();
separator_3.setBackground(Color.LIGHT_GRAY);
textField_1 = new JTextField();
textField_1.setColumns(10);
textField = new JTextField();
textField.setColumns(10);
final JTextArea textArea = new JTextArea(10, 30);
// textArea.setPreferredSize(new Dimension(310, 325));
// textArea.setMinimumSize(new Dimension(310, 325));
textArea.setEditable(false);
textArea.setFont(new Font("Tahoma", Font.PLAIN, 11));
textArea.setText("some info");
final JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setVisible(false);
JButton btnNewButton = new JButton("RUN!");
btnNewButton.addMouseListener(new MouseAdapter()
{
@Override
public void mouseClicked(MouseEvent e)
{
lblResults.setText("RESULTS:");
if ((!rdbtnDreadItem.isSelected() && !isNumeric(textField.getText())) || !isNumeric(textField_1.getText()) || textField.getText().startsWith("0") || textField_1.getText().startsWith("0") || textField.getText().equals("100"))
{
textArea.setText("Rate and scrolls must be valid numbers.");
}
else
{
textArea.setText(doYourMagic(rdbtnDreadItem.isSelected() ? 60 : Integer.parseInt(textField.getText()), Integer.parseInt(textField_1.getText()), rdbtnDreadItem.isSelected()));
// scrollPane.setViewportView(textArea);
scrollPane.setVisible(true);
}
}
});
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addComponent(separator, GroupLayout.DEFAULT_SIZE, 343, Short.MAX_VALUE)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(185)
.addComponent(separator_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addContainerGap(158, Short.MAX_VALUE))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(103)
.addComponent(lblRateTester)
.addContainerGap(120, Short.MAX_VALUE))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(30)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(lblPleaseEnterRate)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)
.addGap(16)
.addComponent(lblScrolls)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
.addGap(18)
.addComponent(rdbtnDreadItem))
.addComponent(separator_3, GroupLayout.PREFERRED_SIZE, 272, GroupLayout.PREFERRED_SIZE))
.addContainerGap(41, Short.MAX_VALUE))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(135)
.addComponent(btnNewButton)
.addContainerGap(151, Short.MAX_VALUE))
.addGroup(gl_contentPane.createSequentialGroup()
.addContainerGap()
.addComponent(separator_2, GroupLayout.PREFERRED_SIZE, 309, GroupLayout.PREFERRED_SIZE)
.addContainerGap(24, Short.MAX_VALUE))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(136)
.addComponent(lblResults)
.addContainerGap(162, Short.MAX_VALUE))
.addGroup(Alignment.LEADING, gl_contentPane.createSequentialGroup()
.addContainerGap()
.addComponent(textArea, GroupLayout.PREFERRED_SIZE, 299, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 16, GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(lblRateTester)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(separator, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(lblPleaseEnterRate)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(lblScrolls, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, 14, GroupLayout.PREFERRED_SIZE))
.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, 14, GroupLayout.PREFERRED_SIZE)
.addComponent(rdbtnDreadItem, GroupLayout.PREFERRED_SIZE, 14, GroupLayout.PREFERRED_SIZE))
.addGap(18)
.addComponent(separator_3, GroupLayout.PREFERRED_SIZE, 2, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(btnNewButton)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(separator_2, GroupLayout.PREFERRED_SIZE, 2, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(lblResults)
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(textArea, GroupLayout.PREFERRED_SIZE, 163, GroupLayout.PREFERRED_SIZE)
.addGap(18)
.addComponent(separator_1, GroupLayout.PREFERRED_SIZE, 2, GroupLayout.PREFERRED_SIZE))
.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 163, GroupLayout.PREFERRED_SIZE)))
);
contentPane.setLayout(gl_contentPane);
}
private String doYourMagic(int rate, int scrolls, boolean dread)
{
int success = 0;
int row = 0, highRow = 0;
String magic = "";
Random rand = new Random();
for (int i = 0; i < scrolls; i++)
{
int chance = rand.nextInt(99);
if (dread && row > 0)
rate = rate - (5 * row);
if (chance >= rate)
{
row = 0;
}
else
{
success++;
if (++row > highRow)
highRow = row;
}
magic = magic + "SCROLL USED: <--- Roll: " + chance + " <--- Scroll Rate: " + rate + "% <--- " + (row > 0 ? "Success" : "Fail") + "\r\n";
if (dread) // reset dread rate
rate = 60;
}
magic = magic + "\r\n~~~~ TEST RESULTS OF " + rate + "% RATE USING " + scrolls + " SCROLLS ~~~~\r\n";
magic = magic + "Total Success: " + success + "\r\n";
magic = magic + "Highest Success Row: " + highRow + "\r\n";
return magic;
}
private boolean isNumeric(String str)
{
try
{
Integer.parseInt(str);
}
catch (NumberFormatException nfe)
{
return false;
}
return true;
}
}
修改强>
示例sscce在GUI中显示JTextArea,使用更简单的布局:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class ScrollPaneEg {
public static final String LOREM_IPSUM = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, "
+ "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, "
+ "quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. "
+ "Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. "
+ "Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. ";
public static void main(String[] args) {
final JTextArea textarea = new JTextArea(10, 30);
textarea.setWrapStyleWord(true);
textarea.setLineWrap(true);
textarea.setEditable(false);
textarea.setFocusable(false);
JScrollPane scrollpane = new JScrollPane(textarea);
scrollpane
.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JPanel topPanel = new JPanel();
topPanel.add(scrollpane);
topPanel.add(new JButton(new AbstractAction("Press Me") {
@Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < 200; i++) {
textarea.append(LOREM_IPSUM);
}
}
}));
JPanel mainPanel = new JPanel(new BorderLayout(4, 4));
mainPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
mainPanel.add(topPanel, BorderLayout.PAGE_START);
mainPanel.add(scrollpane, BorderLayout.CENTER);
JFrame frame = new JFrame("Layout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
}