我正在尝试运行下面应该显示背景颜色PINK和字符串的程序“这是一个测试。”在白色
1)Test.java
TextToSpeech ttsObject;
private List<WordListModel> mWordList;
private Context context;
private Activity activity;
private int result;
public WordListAdapter(TextToSpeech ttsObject, int result) {
this.ttsObject = ttsObject;
this.result = result;
}
public WordListAdapter(List<WordListModel> mWordList, Context context, TextToSpeech ttsObject, int result) {
this.mWordList = mWordList;
this.context = context;
activity = (Activity)context;
this.result = result;
this.ttsObject = ttsObject;
}
@Override
public WordListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.word_list_layout,parent,false);
return new WordListViewHolder(view);
}
@Override
public void onBindViewHolder(final WordListViewHolder holder, final int position) {
final WordListModel wordmeaning = mWordList.get(position);
holder.mNameTv.setText(wordmeaning.getmWord());
holder.mNameTv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (result == TextToSpeech.LANG_NOT_SUPPORTED || result == TextToSpeech.LANG_MISSING_DATA){
Toast.makeText(activity,"Feature Not Support in your Device",Toast.LENGTH_LONG).show();
}else {
ttsObject.speak(wordmeaning.getmWord(), TextToSpeech.QUEUE_FLUSH, null);
}
}
});
holder.mMeaningTv.setText(wordmeaning.getmMeaning());
holder.mMeaningTv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (result == TextToSpeech.LANG_NOT_SUPPORTED || result == TextToSpeech.LANG_MISSING_DATA){
Toast.makeText(activity,"Feature Not Support in your Device",Toast.LENGTH_LONG).show();
}else {
ttsObject.speak(wordmeaning.getmMeaning(), TextToSpeech.QUEUE_FLUSH, null);
}
}
});
holder.mFavIv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DatabaseHelper mydb = new DatabaseHelper(context);
try {
if (wordmeaning.getIsFav().equals("false")){
boolean check = mydb.updateData("true",wordmeaning.getmWord(),wordmeaning.getDbTable());
if (check){
holder.mFavIv.setImageResource(R.drawable.ic_star_black_24dp);
wordmeaning.setIsFav("true");
}
Log.d("TAG",Boolean.toString(check));
}else {
boolean check = mydb.updateData("false",wordmeaning.getmWord(),wordmeaning.getDbTable());
if (check){
holder.mFavIv.setImageResource(R.drawable.ic_star_border_black_24dp);
wordmeaning.setIsFav("false");
}
Log.d("TAG",Boolean.toString(check));
}
}catch (Exception e){
Log.d("TAG",e.toString());
}
}
});
if (wordmeaning.getIsFav().equals("false")){
holder.mFavIv.setImageResource(R.drawable.ic_star_border_black_24dp);
}else {
holder.mFavIv.setImageResource(R.drawable.ic_star_black_24dp);
}
}
@Override
public int getItemCount() {
return mWordList.size();
}
2)Screen.java
package Practice;
import java.awt.*;
import javax.swing.JFrame;
public class Test extends JFrame{
public static void main(String[] args){
DisplayMode dm = new DisplayMode(800,600,16,DisplayMode.REFRESH_RATE_UNKNOWN);
Test test = new Test();
test.run(dm);
}
public void run(DisplayMode dm){
setBackground(Color.PINK);
setForeground(Color.WHITE);
setFont(new Font("Arial", Font.PLAIN, 24));
Screen s = new Screen();
try{
s.setFullScreen(dm, this);
try{
Thread.sleep(5000);
}catch(Exception ex){
ex.printStackTrace();
}
}finally{
s.restoreScreen();
}
}
public void paint(Graphics g){
g.drawString("This is a test.", 200, 200);
}
}
预期结果:
全屏显示背景颜色PINK和字符串“这是一个测试。”在白色
实际结果:
全屏显示背景颜色BLACK和String“这是一个测试。”在怀特。
我在Windows机器上的eclipse中运行它。
答案 0 :(得分:1)
正如我在第一条评论中所说,你应该做很多事情:
您不应直接使用JFrame
进行自定义绘画。相反,请使用JPanel
。在这种情况下也无需扩展JFrame
,您不会添加任何功能。
要更改框架的背景颜色,最好设置框架内容窗格的背景颜色。另外,要更改用于绘制字符串的颜色,您应该调用Graphics.setColor ()
。
不要覆盖paint
方法。相反,请覆盖JComponent's
paintComponent
方法。此外,方法中的第一行应调用父绘制方法,以允许组件在执行任何其他操作之前正常绘制自己。
最后,你不清楚你想在Screen
课程中做什么,但是调用Thread.sleep
实际上是一种不好的做法,对我来说,所有代码都没有多大意义。
启用全屏模式的最佳方法是在您的相框上调用setExtendedState (JFrame.MAXIMIZED_BOTH)
。
这是我所说的一个有效例子:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
public class Test
{
public static void main (String [] a) {
SwingUtilities.invokeLater (new Runnable () {
@Override public void run () {
createAndShowGUI ();
}
});
}
private static void createAndShowGUI () {
JFrame frame = new JFrame ("App");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.setContentPane (new DrawPanel (Color.PINK, Color.WHITE));
frame.pack ();
// frame.setExtendedState (JFrame.MAXIMIZED_BOTH); // You can use this instruction to have full screen mode.
frame.setLocationRelativeTo (null);
frame.setVisible (true);
}
}
class DrawPanel extends JPanel
{
Color foregroundColor;
public DrawPanel (Color backgroundColor, Color foregroundColor) {
setBackground (backgroundColor);
this.foregroundColor = foregroundColor;
}
@Override public Dimension getPreferredSize () {
return new Dimension (400, 400);
}
@Override protected void paintComponent (Graphics g) {
super.paintComponent (g);
g.setColor (foregroundColor);
g.setFont (new Font ("Arial", Font.PLAIN, 24));
g.drawString ("This is a test.", 200, 200);
}
}
截图: