Java 如何在jpanel内将jlabel文本向左对齐
我正在尝试将Jlabel向左对齐,但是我没有做到。请告诉我如何解决此问题。如果您对此有任何建议,我们将不胜感激 我尝试了这段代码,但它并没有解决问题Java 如何在jpanel内将jlabel文本向左对齐,java,swing,jpanel,jlabel,layout-manager,Java,Swing,Jpanel,Jlabel,Layout Manager,我正在尝试将Jlabel向左对齐,但是我没有做到。请告诉我如何解决此问题。如果您对此有任何建议,我们将不胜感激 我尝试了这段代码,但它并没有解决问题 Jlabel=new JLabel("Label text",SwingConstants.LEFT); Jlabel.setHorizontalAlignment(SwingConstants.LEFT); 这是我的完整代码 import javax.swing.*; import java.awt.BorderLayout;
Jlabel=new JLabel("Label text",SwingConstants.LEFT);
Jlabel.setHorizontalAlignment(SwingConstants.LEFT);
这是我的完整代码
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class MyGui1 extends JPanel implements ActionListener {
/**
*
*/
static JScrollPane jsp;
protected JPanel panel;
protected JTextArea textarea;
static JFrame frame;
JLabel Jlabel=null;
public MyGui1() {
//to lay out the container's components in a rectangular grid
super(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.HORIZONTAL;
JButton jbutton=new JButton("button");
jbutton.setActionCommand("button");
jbutton.addActionListener(this);
add(jbutton,c);
c.gridy=1;
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
setBackground(Color.cyan);
panel=new JPanel();
panel.setLayout(new BoxLayout(panel,BoxLayout.PAGE_AXIS));
jsp=new JScrollPane(panel);
jsp.setPreferredSize(new Dimension(300,300));
jsp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jsp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
add(jsp, c);
setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
}
public void actionPerformed(ActionEvent evt) {
if("button".equals(evt.getActionCommand())){
execute();
}
}
synchronized public void execute(){
// to remove all the content of panel
panel.removeAll();
// to refresh the window
for(int i=0;i<20 ;i++){
Jlabel=new JLabel("Labe1l"+i,SwingConstants.LEFT);
// Jlabel.setAlignmentX(CENTER_ALIGNMENT);
// Jlabel.setAlignmentY(LEFT_ALIGNMENT);
textarea=new JTextArea();
textarea.setText("sample text");
if(i==2){
textarea.setText("sample text........................\nsample text.................. ");
}
if(i==5){
textarea.setText("sample text.\nsample text.sample text.\nsample text. ");
}
if(i==7){
textarea.setText("sample text.\nsample text.sample text.\nsample text sample text..\nsample text.sample text..\nsample text. ");
}
textarea.append("\n");
textarea.setEditable(false);
Jlabel.setLayout(new BorderLayout());
Jlabel.setHorizontalAlignment(SwingConstants.LEFT);
panel.add(Jlabel);
// in order to wrap up the data in text area
textarea.setLineWrap(true);
panel.add(textarea);
Jlabel.setPreferredSize(new Dimension(240,30));
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
// textarea.setPreferredSize(getMinimumSize());
// panel.setPreferredSize(getSize());
panel.revalidate();
panel.repaint();
jsp.getVerticalScrollBar().setValue(0);
jsp.validate();
}
});
}
jsp.revalidate();
jsp.repaint();
frame.repaint();
frame.revalidate();
}
@SuppressWarnings("static-access")
private static void createAndShowGUI() {
//Create and set up the window.
frame = new JFrame("DesktopSearchEngine");
frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//adding window listener for exit operation
frame.addWindowListener( new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
JFrame frame = (JFrame)e.getSource();
int result = JOptionPane.showConfirmDialog(
frame,
"Are you sure you want to exit the application?",
"Exit Application",
JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.YES_OPTION)
{
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
else if(result==JOptionPane.NO_OPTION){
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}
}
});
//Add contents to the window GUI part and to perform all operations
frame.add(new MyGui1());
//Display the window.
frame.pack();
//to keep the frame visible
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
import javax.swing.*;
导入java.awt.BorderLayout;
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.GridBagConstraints;
导入java.awt.GridBagLayout;
导入java.awt.GridLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.awt.event.WindowAdapter;
导入java.awt.event.WindowEvent;
公共类MyGui1扩展JPanel实现ActionListener{
/**
*
*/
静态JScrollPane jsp;
受保护的JPanel面板;
保护区;
静态JFrame;
JLabel JLabel=null;
公共MyGui1(){
//在矩形栅格中布置容器组件的步骤
超级(新GridBagLayout());
GridBagConstraints c=新的GridBagConstraints();
c、 gridwidth=GridBagConstraints.rements;
c、 填充=GridBagConstraints.HORIZONTAL;
JButton JButton=新JButton(“按钮”);
setActionCommand(“按钮”);
addActionListener(这个);
添加(jbutton,c);
c、 gridy=1;
c、 gridwidth=GridBagConstraints.rements;
c、 fill=GridBagConstraints.BOTH;
c、 权重x=1.0;
c、 权重y=1.0;
挫折背景(颜色:青色);
panel=新的JPanel();
panel.setLayout(新的BoxLayout(panel,BoxLayout.PAGE_轴));
jsp=新的JScrollPane(面板);
setPreferredSize(新维度(300300));
setHorizontalScrollBarPolicy(ScrollPaneConstants.HorizontalScrollBar,根据需要);
setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL\u SCROLLBAR\u根据需要);
添加(jsp,c);
setBorder(BorderFactory.createEmptyByOrder(10,10,10,10));
}
已执行的公共无效操作(操作事件evt){
如果(“按钮”.equals(evt.getActionCommand())){
执行();
}
}
同步的公共void execute(){
//要删除面板的所有内容,请执行以下操作:
panel.removeAll();
//刷新窗口
对于(int i=0;i您可以使用FlowLayout.LEADING将标签包装在JPanel
中
JPanel wrapper = new JPanel(new FlowLayout(FlowLayout.LEADING,0, 0));
wrapper.add(Jlabel);
panel.add(wrapper);
还要记住遵循Java命名约定。变量以小写字母开头,使用驼峰式大小写:Jlabel
→ jLabel
提供图形用户界面的ASCII艺术(或带有简单图形的图像),它应该以最小的尺寸显示,并且(如果可以调整大小)具有额外的宽度/高度。jLabel jLabel
使用Java命名约定-jLabel jLabel
。变量以大写字母开头,使用驼峰大小写。