Java 是否在Joptionpane中显示多行?
嗨,我是这个论坛的新成员,也是java的新手,我需要一些关于java入门作业的帮助。我做了大部分的逻辑。问题是要编写一个程序,显示从100到1000的所有数字,每行10个,可以被5和6整除。数字之间只有一个空格。我的教授想把它放在Joptionpane窗口中。当我尝试这样做时,窗口中只会弹出一个答案。如何使我的答案在一行中显示十个,在一个窗口中用一个空格隔开?我的教授希望我们使用一个转义函数来显示答案的行Java 是否在Joptionpane中显示多行?,java,Java,嗨,我是这个论坛的新成员,也是java的新手,我需要一些关于java入门作业的帮助。我做了大部分的逻辑。问题是要编写一个程序,显示从100到1000的所有数字,每行10个,可以被5和6整除。数字之间只有一个空格。我的教授想把它放在Joptionpane窗口中。当我尝试这样做时,窗口中只会弹出一个答案。如何使我的答案在一行中显示十个,在一个窗口中用一个空格隔开?我的教授希望我们使用一个转义函数来显示答案的行 public class FindFactors { public static
public class FindFactors {
public static void main(String[] args) {
String message = "";
final int NumbersPerLine = 10; // Display 10 numbers per line
int count = 0; // Count the number of numbers divisible by 5 and 6
// Test all numbers from 100 to 1,000
for (int i = 100; i <= 1000; i++) {
// Test if number is divisible by 5 and 6
message = message + " " + i;
count++;
if (count == 10) {
message = message + "\r\n";
count = 0;
}
if (i % 5 == 0 && i % 6 == 0) {
count++; // increment count
// Test if numbers per line is 10
if (count % NumbersPerLine == 0)
JOptionPane.showMessageDialog(null, i);
else
JOptionPane.showMessageDialog(null, (i + " "));
}
}
}
}
公共类FindFactors{
公共静态void main(字符串[]args){
字符串消息=”;
final int NumbersPerLine=10;//每行显示10个数字
int count=0;//计算可被5和6整除的数
//测试100到1000之间的所有数字
对于(int i=100;iAJOptionPane
具有显示图像、文本或任何其他组件的功能,对于这种特殊情况,您可能需要创建自己的JPanel
,它将每一行数字添加到JLabel
中,然后将JLabel
添加到其中
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class MultiLineOptionPane {
private JPanel pane;
public static void main(String[] args) {
SwingUtilities.invokeLater(new MultiLineOptionPane()::createAndShowGui);
}
public void createAndShowGui() {
pane = new JPanel();
pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS));
StringBuilder sb = new StringBuilder(); //This object will be used to concatenate in the next for loop
for (int i = 0; i < 500; i++) { //Sample for loop
if ((i) % 10 == 0) { //Every 10 numbers we restart the StringBuilder
pane.add(new JLabel(sb.toString())); //We add a new JLabel to the JPanel with the contents of the StringBuilder
sb.delete(0, sb.length()); //We restart the StringBuilder
} else {
sb.append(i); //We append the current number to the StringBuilder
sb.append(" "); //We append a space after the number
}
}
pane.add(new JLabel(sb.toString())); //We add the last line of numbers in the StringBuilder to the pane
JOptionPane.showMessageDialog(new JFrame(), pane, "Numbers", JOptionPane.PLAIN_MESSAGE); //We display the message
}
}
导入javax.swing.BoxLayout;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JOptionPane;
导入javax.swing.JPanel;
导入javax.swing.SwingUtilities;
公共类多选项窗格{
私人JPanel窗格;
公共静态void main(字符串[]args){
invokeLater(新的multilitionpane()::createAndShowGui);
}
public void createAndShowGui(){
pane=newjpanel();
pane.setLayout(新的BoxLayout(pane,BoxLayout.PAGE_轴));
StringBuilder sb=new StringBuilder();//此对象将用于在下一个for循环中连接
for(int i=0;i<500;i++){//Sample for循环
如果((i)%10==0){//每10个数字我们就重新启动StringBuilder
add(newjlabel(sb.toString());//我们使用StringBuilder的内容向JPanel添加一个新的JLabel
sb.delete(0,sb.length());//我们重新启动StringBuilder
}否则{
sb.append(i);//我们将当前编号附加到StringBuilder
sb.append(“”;//我们在数字后面加一个空格
}
}
add(new JLabel(sb.toString());//我们将StringBuilder中的最后一行数字添加到窗格中
JOptionPane.showMessageDialog(新建JFrame(),窗格,“数字”,JOptionPane.PLAIN_MESSAGE);//我们显示消息
}
}
上述程序的输出看起来像(被裁剪或太高):
请参阅下面的方法,对您的代码稍作更改,将提供所需的输出
public class FindFactors {
public static void main(String[] args) {
final int NumbersPerLine = 10; // Display 10 numbers per line
int count = 0; // Count the number of numbers divisible by 5 and 6
// Test all numbers from 100 to 1,000
String numbersPerLine = "";
for (int i = 100; i <= 1000; i++) {
// Test if number is divisible by 5 and 6
if (count == 10) {
count = 0;
}
if (i % 5 == 0 && i % 6 == 0) {
numbersPerLine =numbersPerLine+" "+i;
count++; // increment count
// Test if numbers per line is 10
if (count % NumbersPerLine == 0)
numbersPerLine =numbersPerLine+"\n";
}
}
JOptionPane.showMessageDialog(null, numbersPerLine);
}
}
公共类FindFactors{
公共静态void main(字符串[]args){
final int NumbersPerLine=10;//每行显示10个数字
int count=0;//计算可被5和6整除的数
//测试100到1000之间的所有数字
字符串numbersPerLine=“”;
对于(int i=100;我注意到在我的解决方案中,我在一个循环中进行字符串连接,这在性能上不是很有效,但因为它是一个单独的程序,所以我就是这样执行的。对于生产级代码,不应该遵循这种字符串连接方法。首先感谢您提供的解决方案!但是,当我运行您的解决方案时,t第一行有9个答案,我要求在一行中有10个答案。只需删除for循环开头的第一个count++
增量,然后它就可以工作了,并通过删除未使用的字符串变量消息来清理代码。@Shawnkurakose您能检查更新的代码吗,我已经测试过了,它可以正常工作了预计起飞时间。