
正文
java写代码格式左对齐 java对齐方式
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java在文本框内的文字实现左右中对齐方法
JtextArea可以用textArea.setAlignmentX(float alignmentX);来设置文字对齐位置。alignmentX从0到1,取0左对齐,0.5是居中,1是右对齐。这个设置是针对内部的所有文字的对齐方式,如果要设置部分文字的对齐方式,就要考虑其它的了,比如JtextPane。
JtextPane可以直接内嵌html。直接把网页代码写进去,非常方便。
相关问答
Q1: 在Java中按钮事件中实现左对齐,右对齐
你是不是想要这个效果?或者另一种方法是取得JFrame的宽度然后调整x轴就行的
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Exam_10 extends JFrame implements ActionListener{
Container c = null;
JButton btn[]=new JButton[3];
String[] title={"左对齐","中间对齐","右对齐"};
public Exam_10() {
c = this.getContentPane();
c.setLayout(new FlowLayout());
for(int i=0;ibtn.length;i++){
btn[i]=new JButton(title[i]);
c.add(btn[i]);
btn[i].addActionListener(this);
}
this.setSize(500,300);
this.setVisible(true);
this.setDefaultCloseOperation(3);
}
public static void main(String[] args) {
new Exam_10();
}
public void actionPerformed(ActionEvent e) {
for(int i=0;ibtn.length;i++){
if(e.getSource()==btn[i]){
System.out.println("你点击了"+title[i]+"布局");
switch(i){
case 0:
c.setLayout(new FlowLayout(FlowLayout.LEFT));
break;
case 1:
c.setLayout(new FlowLayout(FlowLayout.CENTER));
break;
case 2:
c.setLayout(new FlowLayout(FlowLayout.RIGHT));
break;
}
super.repaint();
this.setVisible(true);
}
}
}
}
Q2: java标准化格式输出 在控制台输出类似表格类型的左对齐格式
在jsp页面中,如何将数据导出为doc、xls格式的文件。
前台
c:forEach items="${requestScope.newsList.result}" var="list"
tr
td width="5%" style="text-align:center;"span${list.id}/span/td
td width="35%" style="text-align:left; text-indent:2em;"
a href="#" title="${list.newsContent}"span${list.newsTitle}/span/a
/td
td width="15%" style="text-align:center;"span${list.newsCol }/span/td
td width="15%" style="text-align:center;"span${list.newsAuthor }/span/td
td width="20%" style="text-align:center;"span${list.newsDate }/span/td
td width="10%" style="text-align:center;"
div style="float:left;width:50px;height:17px;background:url(../images/IcoBtn.gif)no-repeat;border: 0;background-position: -222px 0px;"
a href="NewsAction?id=${list.id}action=editnews"
img width="50" height="17" style="border:0;" src="../images/Empty.gif" //a
/divinput type="checkbox" name="chk" value="${list.id}" /
/td
/tr
/c:forEach
预期想实现的效果:当我点击某一个超链接的时候,跳转到一个servlet中,在里面进行相应的处理,处理完成后返回原页面。
提问:1、如何实现上面所说的效果。
2、点击超链接的时候,那个超链接应该注意些什么。因为在同一在页面中,可能存在多个c:forEach/c:forEach.
3、在servlet里面,调用相应的处理方法的时候应该注意什么.因为在这个项目中,我是基于MVC模式开发的(dao层与数据库层交互、domain层封装javaBean、service层与dao层、web层交互、web层主要负责接收数据、调用处理方法、跳转)
Q3: java怎么将JTextField里的文字设置成右上角右对齐 还有怎么将JTextField里的文字设置成左下角左对齐?
lz java写代码格式左对齐你好
使用JTextFieldjava写代码格式左对齐的setHorizontalAlignment方法 可以设置水平对齐方式
给lz一个例子:
import java.awt.*;
import javax.swing.*;
public class Test extends JFrame{
private JTextField jf;
public Test(){
super("Test");
setLayout(new FlowLayout(FlowLayout.CENTER, 20, 50));
jf = new JTextField(10);
jf.setHorizontalAlignment(JTextField.RIGHT);//右对齐
//jf.setHorizontalAlignment(JTextField.LEFT);//左对齐
getContentPane().add(jf);
setSize(300,200);
setLocationRelativeTo(null);
setVisible(true);
setDefaultCloseOperation(3);
}
public static void main (String[] args) {
new Test();
}
}
右对齐java写代码格式左对齐的运行效果:
希望能帮助你哈
Q4: java中如何实现多行多列的数据输出时左对齐,要实现下面那种输出,程序怎么改,请高手指教,谢谢!
public static void main(String[] args) {
// TODO Auto-generated method stub
for (int j = 1; j = 5; j++) {
for (int i = 1; i = 5; i++) {
System.out.print("\t"+j*i); // \t可以对其
}
System.out.println();
}
}
Q5: java中JTextPane 怎么把不同的行,设置为不同的对齐方式,我尝试了一下,怎么都是左对齐呀!
修改方法:
在JTextPane中使用带HTML格式的文本就可实际各部分文本按照不同格式显示的效果。
实际效果:
修改后的代码:
import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;
public class Test3 {
//TODO 修改代码
private static String message = "p align='left'In the beginning, there was COBOL, /p"
+ "p align='center'then there was FORTRAN, /p"
+ "p align='right'then there was BASIC, ... /p"
+ "and now there is Java.\n";
public static void main(String[] args) {
String title = (args.length == 0 ? "JTextPane Example" : args[0]);
JFrame frame = new JFrame(title);
Container content = frame.getContentPane();
StyleContext context = new StyleContext();
StyledDocument document = new DefaultStyledDocument(context);
Style style = context.getStyle(StyleContext.DEFAULT_STYLE);
StyleConstants.setAlignment(style, StyleConstants.ALIGN_LEFT);// 左对齐
try {
document.insertString(document.getLength(), message, style);
}
catch (BadLocationException badLocationException) {
System.err.println("Oops");
}
SimpleAttributeSet attributes = new SimpleAttributeSet();
StyleConstants.setAlignment(attributes, StyleConstants.ALIGN_RIGHT);//右对齐
document.setParagraphAttributes(0, document.getLength(), style, true);
try {//为什么没有右对齐呀
document.insertString(document.getLength(), "Hello Java",
attributes);
}
catch (BadLocationException badLocationException) {
}
JTextPane textarea = new JTextPane();
//TODO 新增代码开始
textarea.setContentType("text/html");
textarea.setText(message);
//TODO 新增代码结束
textarea.setEditable(false);
//TODO 被注释代码
/*
textarea.setBackground(UIManager.getColor("label.background"));
textarea.setFont(UIManager.getFont("label"));
textarea.setDocument(document);
*/
JScrollPane textAreascrollPane = new JScrollPane(textarea);
content.add(textAreascrollPane, BorderLayout.CENTER);
JLabel AliasNameLabel = new JLabel("Local Name: ");
content.add(AliasNameLabel, BorderLayout.SOUTH);
frame.setSize(300, 200);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
欢迎继续交流。
关于java写代码格式左对齐和java对齐方式的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。






