简体   繁体   English

JPanel Java内部的动态JTextField

[英]Dynamic JTextField Inside JPanel Java

im doing a project where i have to create a bar with some scrolling text... The problem is that i may have to highlight the background of the text and for that im thinking in use JTextField so i can color the background... My current problem is making the JTextField appear inside the JPanel... this is what i have so far (the design): http://i.stack.imgur.com/AdfyZ.png 即时通讯正在做一个项目,我必须创建带有一些滚动文本的栏...问题是我可能必须突出显示文本的背景,为此我正在考虑使用JTextField,以便我可以为背景着色...我当前的问题是使JTextField出现在JPanel内部...这是我到目前为止(设计)的内容: http : //i.stack.imgur.com/AdfyZ.png

在此处输入图片说明

the Red rectangle will be to display the JTextField with the messages and the green rectangle will be to display some advertising, please note that they are only colored now for i make sure where are the JPanels, when the program is done they will became white and the color will be made by the JTextFields... 红色矩形将显示带有消息的JTextField,绿色矩形将显示一些广告,请注意,它们仅是彩色的,因为我确保JPanels在哪里,当程序完成时,它们将变为白色,颜色将由JTextFields制成...

The next problem, may be the moving text, I dont have that implemented yet but i guess it will be easy, i will leave here an ideia for you tell me if im going in a good way or if i should change to a better ideia... So, i want to do something like the news: http://i.stack.imgur.com/rH2je.jpg 下一个问题可能是移动的文本,我还没有实现,但是我想这很容易,我会在这里留下一个想法,您可以告诉我即时消息是否进展顺利,或者我是否应该改用更好的想法...因此,我想做类似新闻的事情: http : //i.stack.imgur.com/rH2je.jpg

在此处输入图片说明

So, i want that my text go from right to the left, i will create a runnable that for every 10-50 ms change the location of my text some points to the left... about the text displaying im thinking in create and LinkedList of JTextField and then show them in the panel... 因此,我希望我的文本从右向左移动,我将创建一个可运行的程序,每10-50毫秒将文本的位置更改为一些指向左侧的位置...关于在impress和linkedList中显示即时消息的文本JTextField,然后在面板中显示它们...

Thank you in advance and if you can help me displaying the JTextField on the panel i will appreciate, alse, if you can tell me if my ideia about the moving text is the most correct or not... Thank you all for yout time! 在此先感谢您,如果您能帮助我在面板上显示JTextField,还可以告诉我关于移动文本的想法是否最正确,谢谢您!

Edit: I have your "MyPanel" code and in the main class of the frame i have a method that add the MyPanel to the panel where i want them to be... 编辑:我有您的“ MyPanel”代码,并且在框架的主类中,我有一个方法将MyPanel添加到我希望它们位于的面板中。

createMsg();
 PanelMsg.repaint();
}
  public void createMsg() {
    MyPanel test = new MyPanel("teste", "1");
    PanelMsg.add(test);      
}

The PanelMsg is the top panel, the problem may be because im not using any layout and that is because im afraid that this dont work so i was trying with free design. PanelMsg是顶部面板,问题可能是因为我没有使用任何布局,那是因为我担心这不起作用,所以我尝试使用免费设计。 I said that im afraid that doesnt work because i need to make the text role and the layouts block their contents... 我说我担心这行不通,因为我需要使文本角色和布局阻止其内容...

Why don't you create your own JPanel with two JLabels. 为什么不使用两个JLabel创建自己的JPanel。 set the background for both labels. 设置两个标签的背景。

public MyPanel extends JPanel{
private JLabel topLabel;
private JLabel bottomLabel;

MyPanel(String news, String ad){

topLabel = new JLabel(news);
bottomLabel = new JLabel(ad);

//set your layout

}

}

Now, add multiple instances of MyPanel to your JPanel. 现在,将MyPanel的多个实例添加到您的JPanel。

public class Main extends JPanel{
Main(){
//other stuffs
MyPanel abc = new MyPanel("Stack", "Overflow");
MyPanel xyz = new MyPanel("Hello", "World");
add(abc);
add(xyz);
//Use Flow layout
//other stuffs
}

} }

im doing a project where i have to create a bar with some scrolling text... The problem is that i may have to highlight the background of the text and for that im thinking in use JTextField so i can color the background... 即时通讯正在做一个项目,我必须创建一个带有一些滚动文本的栏...问题是我可能必须突出显示文本的背景,并且我正在考虑使用JTextField,以便我可以为背景着色...

I would use JLabels. 我会使用JLabels。 You can set the background of a JLabel, you just need to use: 您可以设置JLabel的背景,只需使用:

label.setOpaque(true);

Check out Marquee Panel for a component to do the scrolling for you. 签出选取框面板中的某个组件可以为您进行滚动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM