FlowLayout / GridLayout 结合?

发布于 2024-12-12 19:12:47 字数 1589 浏览 7 评论 0原文

好吧,我正在尝试创建一个程序,该程序从扫描仪获取 System.in,将输入放入字符串中,然后根据输入创建按钮布局。我对 Java 还很陌生,而且有些自学,所以如果这看起来很奇怪,那是因为我不是专业人士:

else if(wordReader.endsWith("\r")){

//Create grid layout (one below other)
GridLayout buttonReturn = new GridLayout(0,i);
contentPane.setLayout(buttonReturn);
System.out.println("This has a new line value");
i++;

    wordReader.replaceAll("\r","");
    buttonVal = new JButton (wordReader);
    buttonVal.addActionListener(new ButtonListener());
    buttonVal.setEnabled(true);

    contentPane.add(buttonVal, BorderLayout.NORTH);
Border emptyBorder = BorderFactory.createEmptyBorder();
buttonVal.setBorder(emptyBorder);
}

else {

    //Create button layout(side by side)
    FlowLayout buttonLayout = new FlowLayout();
    contentPane.setLayout(buttonLayout);
    contentPane.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);

    buttonVal = new JButton (wordReader);
    buttonVal.addActionListener(new ButtonListener());
    buttonVal.setEnabled(true);
    contentPane.add(buttonVal, BorderLayout.NORTH);
    Border emptyBorder = BorderFactory.createEmptyBorder();
    buttonVal.setBorder(emptyBorder);
}

我的问题: 我想要做的是为每个单词创建一个按钮,但以类似于纸张格式的格式显示这些按钮。也就是说,当一个人点击回车按钮时,我不再需要 FlowLayout(并排),而是现在想要将下一个按钮移动到前一个按钮下方,并将回车符移动到下一个按钮。这可能吗?我试图从 FlowLayout 转到 GridLayout 以移动下面的下一个按钮,但从阅读中我什至不确定这是否可能。有办法做到这一点吗?

有没有一种方法可以检测回车按钮而不在控制台中执行,现在我让他们在输入中输入 \r 作为回车而不是按 Enter 键?

我浏览了大量的说明、文章、论坛和问答帖子(包括本网站上的帖子),但似乎找不到任何可以让我做我想做的事情的东西。我稍后将这样做是从文件而不是 System.in 中读取,所以如果这更容易解释,那么请随意尝试:) 再说一遍,我对此很陌生,并且几乎没有接受过正式培训,所以很愚蠢放下行话将不胜感激。非常感谢大家。

Alright I am trying to create a program that takes System.in from a scanner, places the input in a string, then creates a button layout based on the input. I am pretty new with Java and somewhat self taught so if this looks funky it is because I am no pro:

else if(wordReader.endsWith("\r")){

//Create grid layout (one below other)
GridLayout buttonReturn = new GridLayout(0,i);
contentPane.setLayout(buttonReturn);
System.out.println("This has a new line value");
i++;

    wordReader.replaceAll("\r","");
    buttonVal = new JButton (wordReader);
    buttonVal.addActionListener(new ButtonListener());
    buttonVal.setEnabled(true);

    contentPane.add(buttonVal, BorderLayout.NORTH);
Border emptyBorder = BorderFactory.createEmptyBorder();
buttonVal.setBorder(emptyBorder);
}

else {

    //Create button layout(side by side)
    FlowLayout buttonLayout = new FlowLayout();
    contentPane.setLayout(buttonLayout);
    contentPane.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);

    buttonVal = new JButton (wordReader);
    buttonVal.addActionListener(new ButtonListener());
    buttonVal.setEnabled(true);
    contentPane.add(buttonVal, BorderLayout.NORTH);
    Border emptyBorder = BorderFactory.createEmptyBorder();
    buttonVal.setBorder(emptyBorder);
}

My questions:
What I want to do is create a button for every word, but display those buttons in a format similar to a paper format. That is, when a person hits the carriage return button, I no longer want the FlowLayout (side by side) but want to now move the next button with the carriage return below the previous buttons. Is this possible? I was trying to go from FlowLayout to GridLayout to move the next button below, but from reading I am not even sure that is possible. Is there a way to do this?

Is there a way to detect the carriage return button without executing in the console, right now I am having them input \r as a carriage return in their input instead of hitting enter?

I have looked through numerous instruction, articles, forums, and Q&A posts (including on this site) but can't seem to find anything that will let me do what I want to do. I will later be doing this to read from a file instead of System.in, so if that would be easier to explain then feel free to try on that :) Again I am pretty new at this, and have little formal training, so dumbing down the jargon would be appreciated. Thanks so much everyone.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暮倦 2024-12-19 19:12:47

You might look at JTextPane, which supports embedded components. Instead of a literal flow or grid layout, the StyledDocument maintains a conceptual document layout. Examples may be found in How to Use Editor Panes and Text Panes. Additional examples may be found here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文