Reputation: 61
Please help. When I run this GUI the numbers run off the frame. Is there a way I can make it go to the next line? Also how to you add a scroll bar to this? I want to make it scroll vertically and horizontally.
import java.io.*;
import java.util.*;
import java.lang.*;
import java.text.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class prime extends JFrame {
public static void main(String[] args) {
prime frame = new prime();
}
private TextPanel3C panel1;
private JPanel inPanel;
private JTextField inField;
public prime() {
final int width = 500;
final int height = 500;
setSize(width, height);
setTitle("Find Prime Numbers");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel1 = new TextPanel3C();
add(panel1, "Center");
inPanel = new JPanel();
inPanel.add(new JLabel("Enter Your Number", SwingConstants.RIGHT));
inField = new JTextField(20);
ActionListener inListener = new TextListener();
inField.addActionListener(inListener);
inPanel.add(inField);
add(inPanel, "South");
setVisible(true);
}
private class TextListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
String message = inField.getText();
inField.setText("");
panel1.setMessage(message);
}
}
class TextPanel3C extends JPanel {
private String message;
private Color backGroundColor;
public TextPanel3C() {
message = "";
backGroundColor = Color.white;
}
public TextPanel3C(String x, Color background) {
message = x;
backGroundColor = background;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
int width = getWidth();
int height = getHeight();
setBackground(backGroundColor);
g2.setColor(Color.black);
Font x = new Font("TimesNewRoman", Font.BOLD, 20);
g2.setFont(x);
FontMetrics fm = g2.getFontMetrics(x);
g2.drawString(message, 50, 50);
if (!(message.equals(""))) {
g2.drawString(previousPrime(message), 50, 78);
}
}
public void setMessage(String message) {
if (isPrime(Integer.parseInt(message))) {
this.message = message + " is a prime number.";
} else {
this.message = message + " is not a prime number.";
}
repaint();
}
public boolean isPrime(int num) {
for (int i = 2; i < num; i++) {
if (num % i == 0) {
return false;
}
}
if (num < 2) {
return false;
}
return true;
}
public String previousPrime(String message) {
String totalPrimeNum = "";
int finalNum = Integer.parseInt(message.substring(0, message.indexOf(" ")));
int count = 0;
for (int i = 2; i < finalNum; i++) {
if (isPrime(i)) {
totalPrimeNum += " " + i;
count++;
}
if (count == 10) {
totalPrimeNum += "\n";
count = 0;
}
}
if (isPrime(Integer.parseInt(message.substring(0, message.indexOf(" "))))) {
totalPrimeNum += " " + finalNum;
}
System.out.println(totalPrimeNum);
return totalPrimeNum;
}
}
}
Upvotes: 1
Views: 98
Reputation: 285403
JTextArea myTextArea = new JTextArea(20, 40);
for 20 rows and 40 columns.myTextArea.append(someText + "\n");
JScrollPane scrollPane = new JScrollPane(myTextArea);
. Then again add the scrollPane to the GUI, not the JTextArea. Note that you're not actually adding the JTextArea to the JScrollPane but rather to the scrollpane's viewport.Upvotes: 5